title: MSVC++ optimizing away volatile static
body: <p>I have a Visual Studio solution where certain "plugin" classes register with a factory class, so that the "plugins" can be created by name. These plugins, with the factory, reside in a static library project.</p>
  
  <p>The registering takes place through a volatile static registering template class, each in its own plugin compilation unit (thus the registering process is "done" by each plugin, and there is no central information of the available plugins) like:</p>
  
  <pre><code>volatile static StaticPluginRegisterHelper&lt;PluginClass&gt; s_register;
  </code></pre>
  
  <p>but the problem is that if the PluginClass is not used verbatim elsewhere in the code, the linker opts to optimize the code away, i.e. the static above NEVER gets executed.</p>
  
  <p>This seems to me like a compiler or linker bug, as I have told specifically that the static is volatile (i.e. "DON'T TOUCH!") :)</p>
  
  <p>The workaround is obviously to move the registering above to a compilation unit that I KNOW is included always, and that of course works. It is however, not as neat.</p>
  
  <p>Ideas?</p>