From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-07-20 12:52:27


From: "Greg Colvin" <gcolvin_at_[hidden]>
> I notice that http://users.utu.fi/sisasa/oasis/cppfaq/ctors.html#[10.10]
> recommends the following idiom:
>
> Fred& x()
> {
> static Fred* ans = new Fred();
> return *ans;
> }
>
> when I would have expected
>
> Fred& x()
> {
> static Fred ans;
> return ans;
> }
>
> or am I missing something (as usual)?
>
> Anyway, a simple template for this idiom might be a useful addition to
> Boost.

See the book Modern C++ Design by Andrei Alexandrescu for a way to implement a
rather generic singleton template.

I believe a simple template might cause more problems than it would solve. For
instance, neither of the above are thread safe, and the destruction order can
still cause problems.