确定一个类是否具有函数

时间:2009-08-25 17:41:43

标签: c++ templates sfinae member-functions

使用技巧(由Olivier Langlois描述),我可以确定一个类是否定义了类型:

template<typename T> struct hasType
{
    template<typename C> static char test( typename C::Type );
    template<typename C> static char* test(...);
    enum{ Value= sizeof(test<T>(0))==1 };
};

我还可以确定一个类是否有变量:

template<typename T> struct hasType
{
    template<typename C> static char test( decltype(C::var) );
    template<typename C> static char* test(...);
    enum{ Value= sizeof(test<T>(0))==1 };
};

但是,decltype(c::func) nur decltype(c::func())(取决于参数)都不适用于成员函数。 有没有办法做到这一点,还是我必须在每个类中创建一个仿函数并使用typename C::functor检测它?

编辑:你们都是对的,但是因为我还要测试一个类型,我会使用decltype(&C::func)(显然应该是指针)。

0 个答案:

没有答案