多个类型名称的部分模板专门化 [英] Partial template specialization for more than one typename
问题描述
在下面的代码中,我想考虑具有 void
的函数( Op
考虑返回 true
。类型 Retval
和 Op
的返回值总是匹配。我不能使用这里显示的类型traits区分,并尝试创建基于 Retval
的部分模板专门化失败,由于存在其他模板变量, code> Op 和 Args
。
In the following code, I want to consider functions (Op
s) that have void
return to instead be considered to return true
. The type Retval
, and the return value of Op
are always matching. I'm not able to discriminate using the type traits shown here, and attempts to create a partial template specialization based on Retval
have failed due the presence of the other template variables, Op
and Args
.
变量在模板专业化而不会得到错误?是否有其他方法根据 Op
?的返回类型来改变行为?
How do I specialize only some variables in a template specialization without getting errors? Is there any other way to alter behaviour based on the return type of Op
?
template <typename Retval, typename Op, typename... Args>
Retval single_op_wrapper(
Retval const failval,
char const *const opname,
Op const op,
Cpfs &cpfs,
Args... args) {
try {
CallContext callctx(cpfs, opname);
Retval retval;
if (std::is_same<bool, Retval>::value) {
(callctx.*op)(args...);
retval = true;
} else {
retval = (callctx.*op)(args...);
}
assert(retval != failval);
callctx.commit(cpfs);
return retval;
} catch (CpfsError const &exc) {
cpfs_errno_set(exc.fserrno);
LOGF(Info, "Failed with %s", cpfs_errno_str(exc.fserrno));
}
return failval;
}
推荐答案
template <typename Retval, typename Op, typename... Args>
Retval single_op_wrapper(
Retval const failval,
char const *const opname,
Op const op,
Cpfs &cpfs,
Args... args) {
try {
CallContext callctx(cpfs, opname);
Retval retval;
if (std::is_same<bool, Retval>::value) {
(callctx.*op)(args...);
retval = true;
} else {
retval = (callctx.*op)(args...);
}
assert(retval != failval);
callctx.commit(cpfs);
return retval;
} catch (CpfsError const &exc) {
cpfs_errno_set(exc.fserrno);
LOGF(Info, "Failed with %s", cpfs_errno_str(exc.fserrno));
}
return failval;
}
template<typename Op, typename... Args> void single_op_wrapper<void, Op, Args>(...) {
...
}
b $ b
编辑:忘记你正在写一个函数,而不是一个类。
Forgot you were writing a function, not a class.
这篇关于多个类型名称的部分模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!