[boost] [static] A useful static variable generator.

0 views
Skip to first unread message

Dongfei Yin

unread,
Mar 20, 2009, 1:53:27 AM3/20/09
to bo...@lists.boost.org
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.
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

herve martin

unread,
Mar 20, 2009, 3:46:36 AM3/20/09
to bo...@lists.boost.org
Dongfei Yin wrote:

> 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.
>

quite interesting trick for allocation of static variables !

we could also imagine:
* storing the types list in a tuple to avoid passing type in var<>()
function
* using enum rather than int as template parameter
it could give something like that:

class static_
{
public:
enum static_names { my_string = 0, my_int, my_ptr };
typedef boost::fusion::tuple< std::string, int, void*> static_types;

template < static_names NAME >
static typename boost::fusion::tuple_element<NAME,static_types>::type&
var()
{
static typename boost::fusion::tuple_element<NAME,static_types>::type
instance;
return instance;
}
};

static variable could be retrieved by:
static_::var<static_::my_string>() = ":)";
static_::var<static_::my_int>() = -1618;
static_::var<static_::my_ptr>() = & static_::var<static_::my_string>();

std::cout
<< " my_string: " << static_::var<static_::my_string>()
<< " my_int: " << static_::var<static_::my_int>()
<< " my_ptr: " << static_::var<static_::my_ptr>()
<< std::endl;


just need now to macro-ize the declaration to have something re-usable and
to manage cv qualifiers ...

thanks
herve

Ross Levine

unread,
Mar 20, 2009, 9:41:00 PM3/20/09
to bo...@lists.boost.org
This seems to be a special case of the singleton pattern. Is there any
interest in a boost singleton library? I have some template code I use
for singletons a lot. The programmer need only derive a class from the
singleton class and use CRTP.

Dongfei, as for your code, keep in mind that you cannot control static
deinitialization with that class. You might be interested in the
singleton chapter in Modern C++ Design (Chapter 6).

Reply all
Reply to author
Forward
0 new messages