【发布时间】:2014-12-08 14:06:17
【问题描述】:
我不明白为什么下面的代码不起作用。编译器(gcc)似乎同时实例化了两者 方法和显然整数是有符号或无符号的,所以总是失败。我虽然 enable_if 是为了避免这种情况。
问:为什么会出现编译错误,如何避免?
using namespace boost; // or std as you want
template<typename T>
struct test {
// if signed
template< typename enable_if
< is_signed<T>
, int
>:: type = 0
>
test &operator<<=(int value)
{}
// if unsigned
template< typename enable_if
< is_unsigned<T>
, int
>:: type = 0
>
test &operator<<=(int value)
{}
};
void foo()
{
test<int> x;
x << 1; // COMPILE ERROR no type named 'type' in struct enable_if<unsigned> etc.
test<unsigned> y;
y << 1; // COMPILE ERROR no type named 'type' in struct enable_if<int> etc.
}
【问题讨论】:
-
定义“它不工作”。发生什么了?它与你想要发生的有什么不同?我们不能仅仅从一段代码中神奇地推断出您的意图,您自己承认,该代码不代表该目标。
-
已添加...但与“似乎实现这两种方法...”是多余的
-
根本不是“冗余”,不。我们是科学家。我们重视precision和specifics和data。 “似乎实现了这两种方法”都不是这些东西!
标签: c++ metaprogramming enable-if