gpt4 book ai didi

c++ - 这个部分模板特化有什么问题?

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

template<typename Retval, typename Op, typename... Args>
Retval call_retval_wrapper(CallContext &callctx, Op const op, Args &...args);

template<typename Op, typename ...Args>
bool call_retval_wrapper<bool, Op, Args>(
CallContext &callctx, Op const op, Args &...args) {
(callctx.*op)(args...);
return true;
}

稍后在代码中调用它:

call_retval_wrapper<bool>(callctx, op, args...)

给出这个错误:

src/cpfs/entry.cpp:1908: error: function template partial specialization ‘call_retval_wrapper<bool, Op, Args>’ is not allowed

最佳答案

在 C++ 中,您不能对函数进行部分模板特化,只能对结构和类进行特化。所以你应该要么做完全特化,要么使用具有静态成员函数的类(当然这与函数不同)

你可以使用类的一些技巧:

template<typename Retval, typename Op, typename... Args>
struct my_traits {
static Retval call_retval_wrapper(CallContext &callctx, Op const op, Args &...args);
};

template<typename Op, typename ...Args>
struct my_traits<bool,Op,Args...> {
static bool call_retval_wrapper<bool, Op, Args>(
CallContext &callctx, Op const op, Args &...args) {
(callctx.*op)(args...);
return true;
}
};

template<typename Retval, typename Op, typename... Args>
Retval call_retval_wrapper(CallContext &callctx, Op const op, Args &...args)
{
return my_traits<Retval,Op,Args...>::call_retval_wrapper(calllxtx,op,args...);
}

类似的东西

关于c++ - 这个部分模板特化有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4530253/

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