Say I have the two following test-classes:
struct TestYes {
using type = void;
template <typename... T>
using test = void;
};
struct TestNo { };
and I want to determine if they have this template member test
.
For the member 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;
works perfectly:
static_assert( has_type<TestYes>::value, ""); // OK
static_assert(!has_type<TestNo>::value, ""); // OK
but the equivalent for the template member 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;
fails on
static_assert( has_test<TestYes>::value, "");
I know I can use SFINAE like:
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;
but I'm wondering why the compiler is correctly deducting the partial specialization of has_type_impl
while it remains on the first definition of has_test_impl
.
Thanks in advance for your enlightenment!
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;
};