Opened 13 years ago
Closed 13 years ago
#3859 closed Feature Requests (wontfix)
[Boost.utility]
| Reported by: | Owned by: | No-Maintainer | |
|---|---|---|---|
| Milestone: | Boost 1.42.0 | Component: | utility |
| Version: | Boost 1.41.0 | Severity: | Optimization |
| Keywords: | Cc: |
Description
Having considered http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt I propose singleton implementation.
I had attached "singleton.hpp" to this letter.
Below is the test program:
#include <stdio.h>
#include <singleton.hpp>
class A : public boost::Singleton<A>
{
//
// 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();
}
Test program outputs:
A() foo! ~A()
I suggest include "singleton.hpp" into "utility.hpp" header that it can be used along with: boost::noncopyable, etc.
Attachments (2)
Change History (5)
by , 13 years ago
| Attachment: | singleton.hpp added |
|---|
by , 13 years ago
| Attachment: | singleton.2.hpp added |
|---|
Thread safe "Singleton" pattern implementation - corrected.
comment:1 by , 13 years ago
@@ -13,7 +13,7 @@
*/
template<bool> class CTAssert;
- template<> class CTAssert<false>{ };
+ template<> class CTAssert<true>{ };
/**
See http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt
@@ -39,8 +39,8 @@
SingletonDestroyer()
{
typedef typename boost::is_array<Doomed>::type DoomedArrayType;
- CTAssert<DoomedArrayType::value>
- ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____;
+ bool assert_me = (DoomedArrayType::value == false);
+ CTAssert<assert_me> ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____;
/**
Suppress unused variable warning
*/
comment:2 by , 13 years ago
That is what I mean:
@@ -39,7 +39,7 @@
SingletonDestroyer()
{
typedef typename boost::is_array<Doomed>::type DoomedArrayType;
- bool assert_me = (DoomedArrayType::value == false);
+ const bool assert_me = (DoomedArrayType::value == false);
CTAssert<assert_me> ____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____;
/**
Suppress unused variable warning
comment:3 by , 13 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
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.
A few other points.
- This doesn't conform to Boost naming conventions.
- Double checked locking is broken and this has been well known for ages.
- BOOST_MPL_ASSERT almost always gives better error messages than hand crafted static assertions.

Thread safe "Singleton" pattern implementation.