2017-05-06 70 views

回答

3

声明中的返回类型必须与定义相匹配。

struct A { 
    template <typename T> 
    typename std::enable_if<(sizeof(T) > 4), void>::type 
    foo(T a); 
}; 

SFINAE无法封装为实现细节。

demo)来实现这一

+0

怎么样把启用如果函数参数? – themagicalyang

+0

@themagicalyang当然,同样的区别。 – Potatoswatter

0

一种方式是内部标签调度:

#include <utility> 
#include <iostream> 

struct A { 
    template <typename T> 
    void foo(T a); 

    private: 

    template<class T> 
    auto implement_foo(T value, std::true_type) -> void; 

    template<class T> 
    auto implement_foo(T value, std::false_type) -> void; 
}; 

template <typename T> 
void A::foo(T a) { 
    implement_foo(a, std::integral_constant<bool, (sizeof(T)>4)>()); 
} 

template<class T> 
auto A::implement_foo(T value, std::true_type) -> void 
{ 
    std::cout << "> 4 \n"; 
} 

template<class T> 
auto A::implement_foo(T value, std::false_type) -> void 
{ 
    std::cout << "not > 4 \n"; 
} 


main() 
{ 
    A a; 
    a.foo(char(1)); 
    a.foo(double(1)); 
} 
相关问题