How to write a type trait `is_container` or `is_vector`?

Look, another SFINAE-based solution for detecting STL-like containers: template<typename T, typename _ = void> struct is_container : std::false_type {}; template<typename… Ts> struct is_container_helper {}; template<typename T> struct is_container< T, std::conditional_t< false, is_container_helper< typename T::value_type, typename T::size_type, typename T::allocator_type, typename T::iterator, typename T::const_iterator, decltype(std::declval<T>().size()), decltype(std::declval<T>().begin()), decltype(std::declval<T>().end()), decltype(std::declval<T>().cbegin()), decltype(std::declval<T>().cend()) >, void > > : public std::true_type {}; … Read more

Why should I avoid std::enable_if in function signatures

Put the hack in the template parameters. The enable_if on template parameter approach has at least two advantages over the others: readability: the enable_if use and the return/argument types are not merged together into one messy chunk of typename disambiguators and nested type accesses; even though the clutter of the disambiguator and nested type can … Read more

Why doesn’t SFINAE (enable_if) work for member functions of a class template?

SFINAE only works for deduced template arguments, i.e. for function templates. In your case, both templates are unconditionally instantiated, and the instantiation fails. The following variant works: struct Foo { template <typename T> typename std::enable_if<std::is_same<T, A>::value>::type bar(T) {} // … (further similar overloads) … }; Now Foo()(x) causes at most one of the overloads to … Read more

Selecting a member function using different enable_if conditions

enable_if works because the substitution of a template argument resulted in an error, and so that substitution is dropped from the overload resolution set and only other viable overloads are considered by the compiler. In your example, there is no substitution occurring when instantiating the member functions because the template argument T is already known … Read more

SFINAE to check for inherited member functions

Take a look at this thread: http://lists.boost.org/boost-users/2009/01/44538.php Derived from the code linked to in that discussion: #include <iostream> template <typename Type> class has_foo { class yes { char m;}; class no { yes m[2];}; struct BaseMixin { void foo(){} }; struct Base : public Type, public BaseMixin {}; template <typename T, T t> class Helper{}; … Read more

What is “Expression SFINAE”?

Expression SFINAE is explained quite well in the paper you linked, I think. It’s SFINAE on expressions. If the expression inside decltype isn’t valid, well, kick the function from the VIP lounge of overloads. You can find the normative wording at the end of this answer. A note on VC++: They didn’t implement it completely. … Read more