|
Boost : |
From: John Maddock (John_Maddock_at_[hidden])
Date: 2001-03-27 07:00:46
I posted about this before, but got no responce, so lets try again (there
was a posting blizzard going on at the time!).
The class:
// This is a helper class that provides a way to get a reference to
// an object.
template <class T>
class static_object {
public:
static T& get() {
detail::dummy_constructor d;
static T x(d);
return x;
}
};
Can only be instantiated for types for which static_object is specialised
as the primary template will never compile (because of the
dummy_constructor). However as this type is undocumented it seems wrong to
defining specialisations for this (should I be able to specialise
static_object<char> for example?).
Since this code is never run, how about:
// This is a helper class that provides a way to get a reference to
// an object.
template <class T>
class static_object {
public:
static T& get() {
char d[sizeof(T)];
return *reinterpret_cast<T*>(d);
}
};
- John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk