Boost C++ Libraries

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

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

singleton - Singleton

Introduction

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.

Synopsis

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

Semantics

Symbol Table
Symbol Meaning
T Any class with a non-throwing default constructor and non-throwing destructor

Requirements satisfied by singleton_default<T>
Expression Return Type Assertion/Note/Pre/Post-Condition
singleton_default<T>::instance() T & Returns a reference to the singleton instance

Guarantees

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.

Details

For details on how we provide the guarantees above, see the comments in the header file.

Dependencies

None.

Future Directions

This header may be replaced by a Boost singleton library.


Valid HTML 4.01 Transitional

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)