...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
detail/singleton.hpp provides a way to access a Singleton of a class type. This is not a general Singleton solution! It is restricted in that the class type must have a default constructor.
namespace details { namespace pool { template <typename T> class singleton_default { private: singleton_default(); public: typedef T object_type; static object_type & instance(); }; } // namespace pool } // namespace details
Symbol | Meaning |
---|---|
T | Any class with a non-throwing default constructor and non-throwing destructor |
Expression | Return Type | Assertion/Note/Pre/Post-Condition |
---|---|---|
singleton_default<T>::instance() | T & | Returns a reference to the singleton instance |
The singleton instance is guaranteed to be constructed before main() begins, and destructed after main() ends. Furthermore, it is guaranteed to be constructed before the first call to singleton_default<T>::instance() is complete (even if called before main() begins). Thus, if there are not multiple threads running except within main(), and if all access to the singleton is restricted by mutexes, then this guarantee allows a thread-safe singleton.
For details on how we provide the guarantees above, see the comments in the header file.
None.
This header may be replaced by a Boost singleton library.
Revised 05 December, 2006
Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)