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

Why void_t doesnt work in SFINAE but enable_if does

This seems to be related to CWG issue #1980 (credits to T.C. for correcting me). As a workaround you can define void_t as: template<typename… Ts> struct make_void { typedef void type;}; template<typename… Ts> using void_t = typename make_void<Ts…>::type; (from cppreference) live example on wandbox

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

What is decltype with two arguments?

It’s an comma-separated list of expressions, the type is identical to the type of the last expression in the list. It’s usually used to verify that the first expression is valid (compilable, think SFINAE), the second is used to specify that decltype should return in case the first expression is valid.

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