Boost logo

Boost :

Subject: [boost] [static] A useful static variable generator.
From: Dongfei Yin (yin.dongfei_at_[hidden])
Date: 2009-03-20 01:53:27


Hi,
    Some time, the sequence of static variable initialization always
make something bad.
the piece of code below will useful about this problem.

class static_
{
public:
    template <int N, class T>
    static T& var()
    {
        static T instance;
        return instance;
    }

private:
    ~static_() {}
};

so, if a static variable of std::string is wanted, I will use:

static_::var<0, std::string>() = ":)";

the first template parameter is used for when many static variable of
same type is needed.
most important thing is all the variable will be initialized before use.

Just for fun.

Dongfei.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk