如何专门化模板化类,以采用不带参数的方法函数类型


template<typename T, typename U>
class Caller{};
template<typename T, typename RET> 
class Caller<T, RET()>  {}
template<typename T, typename RET, typename HEAD, typename TAIL>
class Caller<T, RET(HEAD,TAIL...)> : Caller<T, RET(TAIL...)> {}
class MyClass { void foo(int,int){}};
Caller<decltype(&MyClass::foo), decltype(&MyClass::foo)> caller();

我希望这段代码命中第三个定义两次,然后命中第二个定义一次。

我已经尝试了许多变体,它似乎直接跳到最上面的一个,或者抱怨模板类型不够或太多。

它没有选择专业化,因为它不是专门作为对象方法的。 您必须具有void(ClassType::*)(int,int)才能使其成为对象方法。

template<typename A>
class Thing<RET(A::*)()> {
public:
    Thing(int a){printf("In 2n");}
};
template<typename A, typename RET, typename HEAD, typename... TAIL>
class Thing<RET(A::*)(HEAD, TAIL...)> : Thing<RET(A::*)(TAIL...)> {
public:
    Thing(int a) : Thing<RET(A::*)(TAIL...)>(a){printf("In 3n");}
};
class MyClass {public: void foo(int a,int b){}};
// this will print 2, 3, 3
Thing<decltype(&MyClass::foo)> thing(1);

相关内容

最新更新