Inherited from enable_if'd

I am trying to partially specialize a tag for non-character arrays:

template<typename T> struct is_container : std::false_type {}; template<typename T, unsigned N> struct is_container<T[N]> : std::enable_if<!std::is_same<T, char>::value, std::true_type>::type {}; 

Visual Studio 2010 gives me C2039 ( type not an enable_if element ...). However, shouldn't SFINAE be excluded here, and not give a compiler error? Or is SFINAE not applicable in this case?

Of course, I could just highlight specializations for non char and char:

 template<typename T> struct is_container : std::false_type {}; template<typename T, unsigned N> struct is_container<T[N]> : std::true_type {}; template<unsigned N> struct is_container<char[N]> : std::false_type {}; 

But I really would like to know why SFINAE does not work in this particular case.

+6
source share
1 answer

Check out the topic “3.1 Enabling Template Class Specialization” at http://www.boost.org/doc/libs/1_47_0/libs/utility/enable_if.html

Edit: in case the boost.org link dies ...

3.1 Enabling Template Class Specializations Class specialization can be enabled or disabled using enable_if. For enabler expressions, you must add one additional template parameter. This parameter has a default value of void. For instance:

 template <class T, class Enable = void> class A { ... }; template <class T> class A<T, typename enable_if<is_integral<T> >::type> { ... }; template <class T> class A<T, typename enable_if<is_float<T> >::type> { ... }; 

Activating A with any integral type corresponds to the first specialization, while any floating point type coincides with the second. All other types correspond to the main template. The condition can be any Boolean compilation expression that depends on the arguments of the class template. Note that again the second enable_if argument is not needed; the default value (void) is the correct value.

+2
source

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


All Articles