Here is my problem: in a header I define a structure template type_to_string
, which aims at defining a string corresponding to a given type argument:
namespace foo {
template <typename T>
struct type_to_string
{
static const char * value;
};
}
template <typename T>
const char * foo::type_to_string<T>::value = "???";
I also define a default value for the string.
Now, I'd want to use a macro for defining new types:
#define CREATE_ID(name) \
struct name; \
\
template<> \
const char * foo::type_to_string<name>::value = #name;
The problem is that I'd like the macro to be usable in namespaces, as in:
namespace bar
{
CREATE_ID(baz)
}
which is not possible because type_to_string<T>::value
must be defined in a namespace enclosing foo
.
Here is the compilation errors I get:
[COMEAU 4.3.10.1] error: member "foo::type_to_string<T>::value [with T=bar::baz]"
cannot be specialized in the current scope
[VISUAL C++ 2008] error C2888: 'const char *foo::type_to_string<T>::value' :
symbol cannot be defined within namespace 'bar'
with
[
T=bar::baz
]
Strangely, GCC 4.3.5 (MinGW version) doesn't produce any errors.
Does anyone know a workaround for this, maybe by using some lookup rules I'm not aware of (i.e. declaring type_to_string
in the macro so that each namespace has its own version, or something like that)?