Boost C++ Libraries: Ticket #3859: [Boost.utility] https://svn.boost.org/trac10/ticket/3859 <p> Having considered <a class="ext-link" href="http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt"><span class="icon">​</span>http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt</a> I propose singleton implementation. </p> <p> I had attached "singleton.hpp" to this letter. </p> <p> Below is the test program: </p> <pre class="wiki">#include &lt;stdio.h&gt; #include &lt;singleton.hpp&gt; class A : public boost::Singleton&lt;A&gt; { // // Only Singleton class can create and delete us! // SINGLETON_IS_MY_FRIEND; public: void foo(){ printf("foo!\n"); } A(){ printf("A()\n"); } ~A(){ printf("~A()\n"); } }; void main() { // It is not compiled: //A a = A::Instance(); // That is exactly as it should be used: A::Instance().foo(); } </pre><p> Test program outputs: </p> <pre class="wiki">A() foo! ~A() </pre><p> I suggest include "singleton.hpp" into "utility.hpp" header that it can be used along with: boost::noncopyable, etc. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3859 Trac 1.4.3 achinkoff@… Mon, 25 Jan 2010 16:49:31 GMT attachment set https://svn.boost.org/trac10/ticket/3859 https://svn.boost.org/trac10/ticket/3859 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">singleton.hpp</span> </li> </ul> <p> Thread safe "Singleton" pattern implementation. </p> Ticket achinkoff@… Mon, 25 Jan 2010 18:22:58 GMT attachment set https://svn.boost.org/trac10/ticket/3859 https://svn.boost.org/trac10/ticket/3859 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">singleton.2.hpp</span> </li> </ul> <p> Thread safe "Singleton" pattern implementation - corrected. </p> Ticket achinkoff@… Mon, 25 Jan 2010 21:34:16 GMT <link>https://svn.boost.org/trac10/ticket/3859#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3859#comment:1</guid> <description> <pre class="wiki">@@ -13,7 +13,7 @@ */ template&lt;bool&gt; class CTAssert; - template&lt;&gt; class CTAssert&lt;false&gt;{ }; + template&lt;&gt; class CTAssert&lt;true&gt;{ }; /** See http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt @@ -39,8 +39,8 @@ SingletonDestroyer() { typedef typename boost::is_array&lt;Doomed&gt;::type DoomedArrayType; - CTAssert&lt;DoomedArrayType::value&gt; - ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____; + bool assert_me = (DoomedArrayType::value == false); + CTAssert&lt;assert_me&gt; ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____; /** Suppress unused variable warning */ </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 25 Jan 2010 21:46:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3859#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3859#comment:2</guid> <description> <p> That is what I mean: </p> <pre class="wiki">@@ -39,7 +39,7 @@ SingletonDestroyer() { typedef typename boost::is_array&lt;Doomed&gt;::type DoomedArrayType; - bool assert_me = (DoomedArrayType::value == false); + const bool assert_me = (DoomedArrayType::value == false); CTAssert&lt;assert_me&gt; ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____; /** Suppress unused variable warning </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sat, 27 Mar 2010 05:33:47 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3859#comment:3 https://svn.boost.org/trac10/ticket/3859#comment:3 <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">wontfix</span> </li> </ul> <p> New libraries need to go through the review process. Getting singleton right generically is a lot less trivial than it might seem. I believe that one singleton library was rejected a while ago. </p> <p> A few other points. </p> <ol class="loweralpha"><li>This doesn't conform to Boost naming conventions. </li><li>Double checked locking is broken and this has been well known for ages. </li><li>BOOST_MPL_ASSERT almost always gives better error messages than hand crafted static assertions. </li></ol> Ticket