为什么在SFINAE期间需要参数中的指针?

Seo*_*oul 3 c++ templates sfinae c++03 c++98

为什么我需要在线*上制作checker指针

template <typename C> static yes test( checker<C, &C::helloworld>* );

为了使编译时间扣除正常工作,输出1 0

当我删除时*,输出是0 0

#include <iostream>

struct Generic {}; 
struct Hello
{ int helloworld() { return 0; } };

// SFINAE test
template <typename T>
class has_helloworld
{
    typedef char                yes;
    typedef struct {char _[2];}  no; 

    template <typename C, int (C::*)()> struct checker;

    template <typename C> static yes test( checker<C, &C::helloworld>* );
    template <typename C> static no  test(...);

public:
    enum { value = sizeof(test<T>(0)) == sizeof(yes) };
};

int main(int argc, char *argv[])
{
    std::cout << has_helloworld<Hello>::value   << std::endl;
    std::cout << has_helloworld<Generic>::value << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我试图整理这两个帖子的练习:

是否可以编写模板来检查函数的存在?

检查类是否具有给定签名的成员函数

son*_*yao 5

因为它被称为test<T>(0),0如果test将指针作为参数类型(as checker<C, &C::helloworld>*),则可以接受为空指针.

如果删除*以使参数类型为checker<C, &C::helloworld>,test<T>(0)只能匹配,test(...)那么您将始终获得结果0.