Boost C++ Libraries: Ticket #1732: Require clarification on boost::serialization::singleton https://svn.boost.org/trac10/ticket/1732 <p> Hi, </p> <p> I would appreciate some reference to the standards in boost/serialization/singleton.hpp. Specifically why this is supposed to work on "working compilers": </p> <blockquote> <p> BOOST_DLLEXPORT static T &amp; instance; <em> include this to provoke instantiation at pre-execution time static void use(T const &amp;) {} static T &amp; get_instance(){ </em></p> <blockquote> <p> static T t; <em> refer to instance, causing it to be instantiated (and </em> initialized at startup on working compilers) use(instance); return t; </p> </blockquote> <p> } </p> </blockquote> <p> Thanks! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1732 Trac 1.4.3 Sohail Somani <boost-trac@…> Sun, 30 Mar 2008 00:35:51 GMT <link>https://svn.boost.org/trac10/ticket/1732#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1732#comment:1</guid> <description> <p> Argh. Foiled by wiki formatting. The formatted code snippet: </p> <div class="wiki-code"><div class="code"><pre> <span class="n">BOOST_DLLEXPORT</span> <span class="k">static</span> <span class="n">T</span> <span class="o">&amp;</span> <span class="n">instance</span><span class="p">;</span> <span class="c1">// include this to provoke instantiation at pre-execution time</span> <span class="k">static</span> <span class="kt">void</span> <span class="nf">use</span><span class="p">(</span><span class="n">T</span> <span class="k">const</span> <span class="o">&amp;</span><span class="p">)</span> <span class="p">{}</span> <span class="k">static</span> <span class="n">T</span> <span class="o">&amp;</span> <span class="n">get_instance</span><span class="p">(){</span> <span class="k">static</span> <span class="n">T</span> <span class="n">t</span><span class="p">;</span> <span class="c1">// refer to instance, causing it to be instantiated (and</span> <span class="c1">// initialized at startup on working compilers)</span> <span class="n">use</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span> <span class="k">return</span> <span class="n">t</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Mon, 31 Mar 2008 15:55:30 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/1732#comment:2 https://svn.boost.org/trac10/ticket/1732#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> We have found no standards conforming way to guarentee instantiation of code not explicitly refered to. Implementation of BOOST_CLASS_EXPORT requires this facility. Hence, implementation of BOOST_CLASS_EXPORT not guarenteed portable. </p> <p> However, we have been able to implement it on most widely used compilers. Typically this involves "exporting" for access by a DLL so that the compiler won't throw it away even though it's never explicitly referred to. </p> <p> Of course, each compiler is different so its possible that moving this code from one compiler to another will require some sort of enhance to make it work. The current method factors this problem into one place - boost::serialization::singleton - whereas before it was sprinkled all over the place </p> <p> Robert Ramey </p> Ticket