【发布时间】:2016-05-16 00:52:55
【问题描述】:
假设我对调用类方法的两个模板函数有相同的主体,如下所示:
template <typename jsType, typename jsParamType, typename ParamPrivateType = jsParamType::PrivateType, void(PrivateType::*Method)(const ParamPrivateType&)>
static bool SetByRefMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
}
template <typename jsType, typename jsParamType, typename ParamPrivateType = jsParamType::PrivateType, void(PrivateType::*Method)( ParamPrivateType&)>
static bool SetByRefMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
}
我尝试写一次body,那么当被调用的方法有参数const并且当它有参数not @987654324时,让编译器使用它的正确方法是什么@?
【问题讨论】:
-
您可以将函数指针作为模板
typename Func。顺便说一句,您的模板看起来都很奇怪。 -
这只是一个案例,我还有一些其他的函数类型要调用。我绑定了一些我无法触及的第三方 API,因此我需要使用模板进行参数化。
-
这看起来甚至不会编译给我。非默认模板参数不能跟随默认模板参数。
-
他们编译。但是拥有两次身体是多余的......
-
@SamVarshavchik 这有点棘手,但他们可以,原因是可能会发生额外的类型推导,其中一些默认参数可能不会输入。示例here.
标签: c++ templates traits template-argument-deduction