问题描述
请考虑以下示例:
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".
Clang (3.4.1) outputs "-1, -1", while GCC(4.9.0) outputs "-1, 42".
问题在于 pick
的专业化.虽然Gcc似乎很乐意将 is_nice< T> :: value
(42)转换为 bool(true)
,但是clang却不这样做,并放弃了专业化.这两个示例均使用 -std = c ++ 11
编译.
The problem lays in the specialization of pick
. While Gcc seems happy to convert is_nice<T>::value
(42) to bool(true)
, clang does not do so, and discards the specialization. Both examples compiled with -std=c++11
.
哪个编译器是正确的?
推荐答案
This is gcc bug 57891. Conversion of the integral constant 42
to bool
involves a narrowing conversion, which is not allowed in non-type template arguments. Hence the enable_if
is ill-formed, and the pick
specialization should be discarded, as clang correctly does.
§14.3.2/5 [temp.arg.nontype]
§5.19/3 [expr.const]
§8.5.4/7 [dcl.init.list]
此最小示例演示了gcc错误:
template<bool>
struct foo{};
foo<10> f;
int main() {}
gcc-4.9接受代码,而clang-3.4拒绝它并出现以下错误:
gcc-4.9 accepts the code while clang-3.4 rejects it with the following error:
foo<10> f;
^
轻松解决您的特定问题.确保 enable_if
的非类型模板参数的计算结果为 bool
The fix to your particular problem is easy. Make sure the non-type template argument to enable_if
evaluates to a bool
template<class T>
struct pick<T, typename std::enable_if< is_nice<T>::value != 0 >::type >
// ^^^^^^
{
typedef std::integral_constant<int, is_nice<T>::value > type;
};
这篇关于在SFINAE中将int缩小为bool,gcc和clang之间的输出不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!