在了跟头,重新改设计了。。。。
无论is_ar为多少,始终输出 "not"
template<typename T, int is_ar= 1>//ant::is_arithmetic<T>::value >
class Test
{
public:
void test()
{
#if is_ar
cout<<"is\n";
#elif !is_ar
cout<<"not\n";
#endif
cout<<"is_arr="<<is_ar<<endl;
}
};
为什么?
可能原因:
#elif 是预处理过程中解析的,不做表达狮的估值
不过要实现倒可以这样写(但麻烦多了):
template<typename T, int is_ar=ant::is_arithmetic<T>::value >
class Test
{
public:
void test()
{
cout<<"not\n";
}
};
template<typename T>
class Test<T, 1>
{
public:
void test()
{
cout<<"is\n";
}
};

本文探讨了一个关于C++模板元编程中的问题,即如何根据不同类型的属性动态地改变模板类的行为。文章通过具体的代码示例展示了使用预处理器指令和模板特化两种方法来实现这一功能,并解释了为何直接使用预处理器指令无法达到预期效果。
1303

被折叠的 条评论
为什么被折叠?



