【发布时间】:2018-12-17 03:00:12
【问题描述】:
为什么我需要* 才能使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;
}
这是我尝试将这两个帖子放在一起的练习:
Is it possible to write a template to check for a function's existence?
【问题讨论】:
标签: c++ templates sfinae c++03 c++98