【发布时间】:2013-01-26 18:46:38
【问题描述】:
以下程序在使用 GCC 4.7 和 clang 3.2 编译时,会产生“1”作为输出。
#include <type_traits>
struct foo {
template<typename T>
foo(T) {
static_assert(not std::is_same<int, T>(), "no ints please");
}
};
#include <iostream>
int main() {
std::cout << std::is_constructible<foo, int>();
}
这令人困惑。 foo 显然不能从 int 构造!如果我将main 更改为以下内容,两个编译器都会因为静态断言失败而拒绝它:
int main() {
foo(0);
}
为什么两个编译器都说它是可构造的?
【问题讨论】:
-
您应该使用 enable_if 从可能的 foo 中删除整数。
标签: c++ templates c++11 constructor typetraits