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

“What happened to my SFINAE” redux: conditional template class members?

Firstly, C++11 did not carry forward boost’s disable_if. So if you’re going to transition boost code, you’ll need to use enable_if with a negated condition (or redefine your own disable_if construct). Secondly, for SFINAE to reach in and apply to the method level, those methods must be templates themselves. Yet your tests have to be … Read more

How to test whether class B is derived from template family of classes

Try this: #include <type_traits> template <typename T, template <typename> class Tmpl> // #1 see note struct is_derived { typedef char yes[1]; typedef char no[2]; static no & test(…); template <typename U> static yes & test(Tmpl<U> const &); static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes); }; Usage: #include <iostream> template<class T> struct X {}; struct … Read more

Detect operator support with decltype/SFINAE

In C++11 the shortest most general solution I found was this one: #include <type_traits> template<class T, class = decltype(std::declval<T>() < std::declval<T>() )> std::true_type supports_less_than_test(const T&); std::false_type supports_less_than_test(…); template<class T> using supports_less_than = decltype(supports_less_than_test(std::declval<T>())); #include<iostream> struct random_type{}; int main(){ std::cout << supports_less_than<double>::value << std::endl; // prints ‘1’ std::cout << supports_less_than<int>::value << std::endl; // prints ‘1’ std::cout … Read more

SFINAE tried with bool gives compiler error: “template argument ‘T::value’ involves template parameter” [duplicate]

Actually what you’re doing is forbidden by section §14.5.4/9 which says, A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier. The trick could be using a type for second template parameter as well, encapsulating the non-type value, as described … Read more

How to check if a template parameter is an iterator type or not?

How about something like this? template<typename T, typename = void> struct is_iterator { static constexpr bool value = false; }; template<typename T> struct is_iterator<T, typename std::enable_if<!std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type> { static constexpr bool value = true; }; example: #include <iostream> #include <type_traits> #include <vector> template<typename T, typename = void> struct is_iterator { static constexpr bool value … Read more

Assert that code does NOT compile

template<class T>struct sink{typedef void type;}; template<class T>using sink_t=typename sink<T>::type; template<typename T, typename=void>struct my_test:std::false_type{}; template<typename T>struct my_test<T, sink_t<decltype( put code here. Note that it must “fail early”, ie in the signature of a function, not in the body )> >:std::true_type {}; The above generates a test if the “put code here” can be evaluated. To determine … Read more

using SFINAE for template class specialisation

IF the original declaration of User<> can be adapted to template<typename, typename=std::true_type> class User; then we can find a solution (following Luc Danton’s comment, instead of using std::enable_if) template<typename> struct is_Data : std::false_type {}; template<typename T> struct is_Data<Data<T>> : std::true_type {}; template<typename T> class User<T, typename is_Data<T>::type > { /* … */ }; However, this … Read more