请考虑以下示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
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. …