SFINAE的问题

The*_* do 5 c++ templates sfinae

为什么这个代码(M类中的fnc值)不能通过SFINAE规则解决?我收到一个错误:

Error   1   error C2039: 'type' : is not a member of
                                   'std::tr1::enable_if<_Test,_Type>'  
Run Code Online (Sandbox Code Playgroud)

当然类型不是成员,它没有在enable_if的这个通用版本中定义,但是如果bool为真,并且如果它是假的则不实例化那么这不是fnc的这个版本背后的全部想法吗?可以请有人向我解释一下吗?

#include <iostream>
#include <type_traits>

using namespace std;

template <class Ex> struct Null;
template <class Ex> struct Throw;

template <template <class> class Policy> struct IsThrow;

template <> struct IsThrow<Null> {
    enum {value = 0};
};

template <> struct IsThrow<Throw> {
    enum {value = 1};
};

template <template <class> class Derived>
struct PolicyBase {
    enum {value = IsThrow<Derived>::value};
};

template<class Ex>
struct Null : PolicyBase<Null> { };

template<class Ex>
struct Throw : PolicyBase<Throw> { } ;

template<template< class> class SomePolicy>
struct M {

  //template<class T>
  //struct D : SomePolicy<D<T>>
  //{
  //};
  static const int ist = SomePolicy<int>::value;
  typename std::enable_if<ist, void>::type value() const
  {
    cout << "Enabled";
  }

  typename std::enable_if<!ist, void>::type value() const
  {
    cout << "Disabled";
  }
};

int main()
{
    M<Null> m;
    m.value();
}
Run Code Online (Sandbox Code Playgroud)

Geo*_*che 5

SFINAE不适用于非模板功能.相反,你可以使用例如(类的)特殊化或基于重载的调度:

template<template< class> class SomePolicy>
struct M
{
    static const int ist = SomePolicy<int>::value;        
    void value() const { 
        inner_value(std::integral_constant<bool,!!ist>()); 
    }
 private:
    void inner_value(std::true_type) const { cout << "Enabled"; }
    void inner_value(std::false_type) const { cout << "Disabled"; }
};
Run Code Online (Sandbox Code Playgroud)