title: Static initialization with necessary sideeffects is optimized away
body: <p>I want to send a signal to each subclass of a class. 
  To achieve this I am using the signals2 library from boost. Because of the usage of the code I need to use a CRTP. </p>
  
  <p>Minimal example, which is included in a header file:</p>
  
  <pre><code>class A {
      static boost::signals2::signal&lt;void()&gt;&amp; getSignal() {
          static boost::signals2::signal&lt;void()&gt; signal;
          return signal;
      }
  
  public:
      static void sendSignal() {
          getSignal()();
      }
  
  protected:
      template &lt;typename slot_t&gt;
      static boost::signals2::connection addSlot(slot_t slot) {
          return _entityToBeDeleted().connect(slot);
      }
  };
  
  template &lt;typename concrete_B&gt;
  class B : A {
      static boost::signals2::connection _installHandler() {
          return A::addSlot([]() {
              //Do something
          });
      }
  
      static const boost::signals2::connection connection;
  };
  template&lt;typename concrete_B&gt;
  const boost::signals2::connection B&lt;concrete_B&gt;::connection = _installHandler();
  
  class C : public B&lt;C&gt; {
      //Concrete subclass
  };
  </code></pre>
  
  <p>Some client class, that wants to notify the subclasses makes a call to <code>A::sendSignal</code>, which invokes all slots connected to the signal. What exactly the content of this notification is is irrelevant for the purpose of this question. As you can see, the side effect of the static initializer of <code>B&lt;concrete_B&gt;::connection</code> is needed for this to work, but as the object itself is never used (nor needed) the complete initialization is skipped. </p>
  
  <p>The problem is: The call to _installHandler() is completely optimized away by the compiler (I'm using MSVC with Visual Studio 2017). I verified this by writing an additional test case, which explicitly tests if the connection object is valid. When I used the object this way the code worked and all other test cases passed. When I removed the additional test case the others failed again.</p>
  
  <p>I tried declaring the static variable volatile as suggested in an answer to <a href="https://stackoverflow.com/questions/32584406/static-initializer-is-optimized-away-when-it-is-in-a-library">this question</a>, but it did not help.</p>
  
  <p><strong>How do I prevent the initialization of the the static connection object to be optimized away. Or: How do I ensure a function with sideeffects that is created by template instanciation is always called once at the beginning of the executable, even though the returned object is never actually used; ideally during static initialization.</strong></p>
  
  <p>I thought about using the connection somehow in another static initializer of this class (<code>std::cout &lt;&lt; connection.connected() &lt;&lt; '\n';</code> or something like this) that is already safe to be called (because the object this initializes is definitely used), but this seems like a nasty hack.</p>
  
  <p>Edit: I implemented the "hack" and it works flawlessly, nontheless I am interested if there is a way to implement the desired behaviour in a "cleaner" way. </p>