这是我的问题:在头文件中,我定义了一个结构体模板
问题在于我希望宏在命名空间中可用,例如:
type_to_string
,其目的是为给定的类型参数定义相应的字符串:namespace foo {
template <typename T>
struct type_to_string
{
static const char * value;
};
}
template <typename T>
const char * foo::type_to_string<T>::value = "???";
我还为字符串定义了一个默认值。
现在,我想使用宏来定义新类型:
#define CREATE_ID(name) \
struct name; \
\
template<> \
const char * foo::type_to_string<name>::value = #name;
问题在于我希望宏在命名空间中可用,例如:
namespace bar
{
CREATE_ID(baz)
}
这是不可能的,因为type_to_string<T>::value
必须在包含foo
的命名空间中定义。
这是我得到的编译错误:
[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
]
奇怪的是,GCC 4.3.5(MinGW版本)没有产生任何错误。
有人知道解决办法吗?也许可以使用一些我不知道的查找规则来解决(例如在宏中声明type_to_string
,以便每个命名空间都有自己的版本,或者类似的方法)?