【发布时间】:2010-12-25 09:02:33
【问题描述】:
在下面的代码中,我想考虑返回 void 的函数 (Ops),而不是考虑返回 true。 Retval 的类型和Op 的返回值总是匹配的。我无法使用此处显示的类型特征进行区分,并且由于其他模板变量 Op 和 Args 的存在,尝试创建基于 Retval 的部分模板特化失败。
如何在模板特化中只特化一些变量而不会出错?有没有其他方法可以根据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;
}
【问题讨论】:
标签: c++ templates c++11 return-type template-specialization