0
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: 错误: 函数模板部分特化 'call_retval_wrapper<bool, Op, Args>' 是不允许的

4

3 回答 3

1

在 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...);
}

类似的东西

于 2010-12-25T16:32:55.763 回答
0

您可以尝试这样的事情(ideone):

template<typename Retval, typename Op, typename... Args>
struct call{
  static Retval retval_wrapper(Op const op, Args &&...args);
};

template<typename Op, typename ...Args>
struct call<bool, Op, Args...>{
  static bool retval_wrapper(Op const op, Args &&...args){
    return true;
  }
};

int main(){
  call<bool, bool, bool>::retval_wrapper(true, true);
}

我没有阅读完整的 C++0x 规范,但现在可以部分专门化功能吗?

于 2010-12-25T13:12:14.287 回答
0

您还需要在此行中解压缩:

bool call_retval_wrapper<bool, Op, Args...>( 
于 2010-12-25T12:35:37.390 回答