有模板成员 - 模板模板扣除

时间:2015-05-04 03:46:30

标签: c++ substitution template-templates

说我有以下两个测试类:

struct TestYes {
    using type = void;
    template <typename... T>
    using test = void;
};

struct TestNo { };

我想确定他们是否有这个模板成员test

对于会员type

template <typename, typename = void>
struct has_type_impl {
    using type = std::false_type;
};
template <typename T>
struct has_type_impl<T, typename T::type> {
    using type = std::true_type;
};
template <typename T>
using has_type = typename has_type_impl<T>::type;

完美运作:

static_assert( has_type<TestYes>::value, ""); // OK
static_assert(!has_type<TestNo>::value, "");  // OK

但模板成员test的等价物:

template <typename, template <typename...> class = std::tuple>
struct has_test_impl {
    using type = std::false_type;
};
template <typename T>
struct has_test_impl<T, T::template test> {
    using type = std::true_type;
};
template <typename T>
using has_test = typename has_test_impl<T>::type;

失败

static_assert( has_test<TestYes>::value, "");

我知道我可以使用SFINAE:

template <typename T>
struct has_test_impl {
private:
    using yes = std::true_type;
    using no = std::false_type;

    template <typename U, typename... Args>
    static auto foo(int) -> decltype(std::declval<typename U::template test<Args...>>(), yes());

    template <typename> static no foo(...);

public:
    using type = decltype(foo<T>(0));
};

template <typename T>
using has_test = typename has_test_impl<T>::type;

但我想知道为什么编译器正确地扣除了has_type_impl的部分特化,而它仍然是has_test_impl的第一个定义。

提前感谢你的启蒙!

1 个答案:

答案 0 :(得分:2)

template<template<class...> class...>
using void_templ = void;

template <typename, typename = void>
struct has_test_impl {
    using type = std::false_type;
};
template <typename T>
struct has_test_impl<T, void_templ<T::template test>> {
    using type = std::true_type;
};
相关问题