title: How to fix undefined behaviour with considerably small changes in code
body: <p>In the core of our project we have following code:</p>
  <pre><code>template&lt;typename T&gt;
  T&amp; get_factory_instance(bool reset = false)
  {
     static boost::scoped_ptr&lt;T&gt; factory_instance;
     if (reset)
     {
        factory_instance.reset();
        return *(T*)0;
     }
     if (!factory_instance)
     {
        factory_instance.reset(new T());
     }
     return *factory_instance;
  }
  </code></pre>
  <p>If <code>reset</code> is <code>true</code>, that it's UB according to standard.</p>
  <p>This function with argument <code>true</code> is called only when return value is ignored, so, memory is not accessed. It's mandatory, function cannot be called from services, only from libraries, when we add call of this function to singleton list of functions with signature <code>void()</code>. We need such strange hack for clearing prepared statement connected to the database, if connection lost.</p>
  <p>So, basically question is: can it fire, if we don't access this memory? And if yes, how can we possibly fix it without rewriting all code dependent on this function, if there is any possibility?</p>
  <p>When <code>T</code> constructor is called it constructs prepared statement and open/use connection to the database, so, creating dummy object is not pretty idea.</p>
  <p>We have near 80 calls of this function with argument <code>true</code> in out libraries. It was introduced in 2015. Code that uses this function without argument in most cases looks like:</p>
  <pre><code>fields_t&amp; fields = get_factory_instance&lt;fields_t&gt;();
  </code></pre>
  <p>Thanks in advance.</p>