gpt4 book ai didi

c++ - 多个类型名的部分模板特化

转载 作者:行者123 更新时间:2023-11-28 08:24:27 25 4
gpt4 key购买 nike

在下面的代码中,我想考虑返回 void 的函数 (Op),而不是考虑返回 trueRetval 类型和 Op 的返回值总是匹配的。我无法使用此处显示的类型特征进行区分,并且由于存在其他模板变量 Op,基于 Retval 创建部分模板特化的尝试失败了> 和 Args

如何在模板特化中只特化一些变量而不会出错?有没有其他方法可以根据 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>(...) {
...
}

编辑:忘了你写的是一个函数,而不是一个类。

关于c++ - 多个类型名的部分模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com