相关疑难解决方法(0)

在SFINAE中缩小int到bool,gcc和clang之间的输出不同

请考虑以下示例:

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. …

c++ gcc clang sfinae c++11

10
推荐指数
1
解决办法
331
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

gcc ×1

sfinae ×1