title: C++: How to get rid of lazy initialization
body: <p>To log errors I have the following code that can be called:</p>
  
  <pre><code>void writeErrorToLog(const LogMessage&amp; message)
      {
          static std::ofstream logFile(ERROR_LOG_FILEPATH, std::ofstream::out | std::ofstream::trunc);
          logFile &lt;&lt; message.getLogMessage();
          logFile.flush();
      }
  </code></pre>
  
  <p>So after the program shuts down, the log file contains all errors that happend during the last run. This works fine, unless no errors occurred. In the case of no error, the log file contains the errors of the run before the last one, as due to lazy initialization, the file was never opened (with the trunc option). Any way to force the static variable to be initialized?</p>