Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
C++ Boost

singleton - Singleton


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

    typedef T object_type;

    static object_type & instance();

} // namespace pool
} // namespace details


Symbol Table
TAny class with a non-throwing default constructor and non-throwing destructor

Requirements satisfied by singleton_default<T>
ExpressionReturn TypeAssertion/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.



Future Directions

This header may be replaced by a Boost singleton library.

Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)

This file can be redistributed and/or modified under the terms found in copyright.html

This software and its documentation is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.