如何专门化模板化 class 以采用不带参数的方法函数类型?
How to specialize a templatized class for taking a method function type that takes no parameters?
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 2\n");}
};
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 3\n");}
};
class MyClass {public: void foo(int a,int b){}};
// this will print 2, 3, 3
Thing<decltype(&MyClass::foo)> thing(1);
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 2\n");}
};
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 3\n");}
};
class MyClass {public: void foo(int a,int b){}};
// this will print 2, 3, 3
Thing<decltype(&MyClass::foo)> thing(1);