title: What's the C++ idiom equivalent to the Java static block?
body: <p>I have a class with some static members, and I want to run some code to initialize them (suppose this code cannot be converted into a simple expression). In Java, I would just do</p>
  
  <pre><code>class MyClass {
      static int myDatum;
  
      static {
          /* do some computation which sets myDatum */
      }
  }
  </code></pre>
  
  <p>Unless I'm mistaken, C++ does not allow for such static code blocks, right? What should I be doing instead?</p>
  
  <p>I would like solution for both of the following options:</p>
  
  <ol>
  <li>Initialization happens when process loads (or when the DLL with this class is loaded).</li>
  <li>Initialization happens when the class is first instantiated.</li>
  </ol>
  
  <p>For the second option, I was thinking of:</p>
  
  <pre><code>class StaticInitialized {
      static bool staticsInitialized = false;
  
      virtual void initializeStatics();
  
      StaticInitialized() {
          if (!staticsInitialized) {
              initializeStatics();
              staticsInitialized = true;
          }
      }
  };
  
  class MyClass : private StaticInitialized {
      static int myDatum;
  
      void initializeStatics() {
          /* computation which sets myDatum */
      }
  };
  </code></pre>
  
  <p>but that's not possible, since C++ (at the moment?) does not allow initialization of non-const static members. But, at least that reduces the problem of a static block to that of static initialization by expression...</p>