6

我正在尝试为非字符数组部分专门化一个特征:

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 给了我一个 C2039(type不是enable_if...的元素)。但是,SFINAE 不应该只是在这里触底而不是给出编译器错误吗?或者 SFINAE 不适用于这种情况?

当然,我可以将 non-char 和 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 {};

但我真的很想知道为什么 SFINAE 在这种特殊情况下不起作用。

4

1 回答 1

2

Check the topic '3.1 Enabling template class specializations' at http://www.boost.org/doc/libs/1_47_0/libs/utility/enable_if.html

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

3.1 Enabling template class specializations Class template specializations can be enabled or disabled with enable_if. One extra template parameter needs to be added for the enabler expressions. This parameter has the default value void. For example:

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> { ... };

Instantiating A with any integral type matches the first specialization, whereas any floating point type matches the second one. All other types match the primary template. The condition can be any compile-time boolean expression that depends on the template arguments of the class. Note that again, the second argument to enable_if is not needed; the default (void) is the correct value.

于 2011-09-12T15:11:06.370 回答