为了回应 .. 某处的其他问题,我编写了这段代码。
struct no_type{};
template<typename T> struct has_apply {
static decltype(T().apply<0u>(double())) func( T* ptr );
static no_type func( ... );
static const bool result = !std::is_same<no_type, decltype(func(nullptr))>::value;
};
class A {
public:
template< unsigned n >
void apply( const double& );
};
class B {
};
int main()
{
std::cout << std::boolalpha << has_apply< A >::result << '\n';
std::cout << std::boolalpha << has_apply< B >::result << '\n';
std::cin.get();
return( 0 );
}
现在在我看来,如果 T 提供了一个接受双右值和模板参数文字的非静态成员函数“apply”,则结果应该为真,否则为假。但是,在编译时,给出的示例实际上无法为 B 类编译has_apply<B>
。在 decltype 语句中替换 T 失败的事实不应该意味着它只是调用另一个函数吗?这不是 SFINAE 的重点吗?
以最荒谬、最无意义的方式解决:
struct no_type{};
template<typename T> struct has_apply {
template<typename U> static decltype(U().apply<0u>(double())) func( U* );
template<typename U> static no_type func( ... );
static const bool result = !std::is_same<no_type, decltype(func<T>(nullptr))>::value;
};
class A {
public:
template< unsigned n >
void apply( const double& );
};
class B {
};
int main()
{
std::cout << std::boolalpha << has_apply< A >::result << '\n';
std::cout << std::boolalpha << has_apply< B >::result << '\n';
std::cin.get();
return( 0 );
}