How to check class template template interface

I tried to overload the template class with SFINAE based on what type of template template had the type typedefined in it (e.g. std::remove_referencehas a member type alias type), but I can't figure out a good way to do this.

For example, I wanted to do

template <template <typename...> class Trait>
using EnableIfHasTypeMember = std::void_t<Trait::type>;

template <template <typename...> class Trait, typename OtherStuff,
          EnableIfHasTypeMember<Trait>* = nullptr>
class Something { ... }

But this gives me a compiler error. Is there a way in which I can check the template template interface?

+4
source share
2 answers

, , , . , , , . :

template <typename T>
struct my_trait
{
    using type = int;
};

template <>
struct my_trait<double> {};

, EnableIfHasTypeMember ? , .

, my_trait<T> T . ; , , , , .

+9

@Jason R , .

type, , .

// primary template handles types that have no nested ::type member:
template< class, class = std::void_t<> >
struct has_type_member : std::false_type { };

// specialization recognizes types that do have a nested ::type member:
template< class T >
struct has_type_member<T, std::void_t<typename T::type>> : std::true_type { };

// specialization recognizes class templates that do have a nested ::type member:
template< template< class ... > class T, class ... Args >
struct has_type_member<T<Args...>, std::void_t<typename T<Args...>::type>> : std::true_type { };
+1

Source: https://habr.com/ru/post/1672377/


All Articles