How do we use void_t for SFINAE?

1. Primary Class Template When you write has_member<A>::value, the compiler looks up the name has_member and finds the primary class template, that is, this declaration: template< class , class = void > struct has_member; (In the OP, that’s written as a definition.) The template argument list <A> is compared to the template parameter list of … Read more

Using SFINAE to check for global operator

I should have simply been more faithful to this answer. A working implementation is: namespace has_insertion_operator_impl { typedef char no; typedef char yes[2]; struct any_t { template<typename T> any_t( T const& ); }; no operator<<( std::ostream const&, any_t const& ); yes& test( std::ostream& ); no test( no ); template<typename T> struct has_insertion_operator { static std::ostream … Read more

Multiple SFINAE class template specialisations using void_t

There is a rule that partial specializations have to be more specialized than the primary template – both of your specializations follow that rule. But there isn’t a rule that states that partial specializations can never be ambiguous. It’s more that – if instantiation leads to ambiguous specialization, the program is ill-formed. But that ambiguous … Read more

SFINAE works differently in cases of type and non-type template parameters

SFINAE is about substitution. So let us substitute! template< typename T, std::enable_if_t<std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} template< typename T, std::enable_if_t<!std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} Becomes: template< class T=int, int* = nullptr> void Add(int) {} template< class T=int, Substitution failure* = nullptr> void Add(int) { template< class T=double, Substitution failure* … Read more

Template specialization and enable_if problems [duplicate]

Default template arguments are not part of the signature of a function template. So in your example you have two identical overloads of less, which is illegal. clang complains about the redefinition of the default argument (which is also illegal according to §14.1/12 [temp.param]), while gcc produces the following error message: error: redefinition of ‘template<class … Read more

check if member exists using enable_if

This has become way easier with C++11. template <typename T> struct Model { vector<T> vertices; void transform( Matrix m ) { for(auto &&vertex : vertices) { vertex.pos = m * vertex.pos; modifyNormal(vertex, m, special_()); } } private: struct general_ {}; struct special_ : general_ {}; template<typename> struct int_ { typedef int type; }; template<typename Lhs, … Read more

tech