【发布时间】:2014-08-12 06:45:43
【问题描述】:
考虑以下示例:
template<int i>
struct nice_type;
template<class T>
struct is_nice : std::false_type {};
template<int i>
struct is_nice< nice_type<i> > : std::integral_constant<int, i> {};
template<class T, class = void>
struct pick
{
typedef std::integral_constant<int, -1> type;
};
template<class T>
struct pick<T, typename std::enable_if< is_nice<T>::value >::type >
{
typedef std::integral_constant<int, is_nice<T>::value > type;
};
int main()
{
std::cout << pick<int>::type::value << ", ";
std::cout << pick< nice_type<42> >::type::value << std::endl;
return 0;
}
Clang (3.4.1) 输出“-1, -1”,而 GCC(4.9.0) 输出“-1, 42”。
问题在于pick 的特化。虽然 Gcc 似乎很乐意将 is_nice<T>::value (42) 转换为 bool(true),但 clang 并没有这样做,并且放弃了专业化。两个例子都是用-std=c++11编译的。
哪个编译器是正确的?
【问题讨论】:
-
第 8 行的意思是
nice_type而不是cool_type?
标签: c++ gcc c++11 clang sfinae