Matt Joiner
Matt Joiner

Reputation: 118470

What's wrong with this partial template specialization?

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;
}

Calling this later in the code:

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

Gives this error:

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

Upvotes: 0

Views: 329

Answers (3)

Artyom
Artyom

Reputation: 31233

In C++ you can't do partial template specialization for functions, only for structures and classes. So you should either to do full specialization or use classes with static member functions (of course this is not same as functions)

You may use some tricks using classes:

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

Something like that

Upvotes: 1

Pawel Zubrycki
Pawel Zubrycki

Reputation: 2713

You can try something like this (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);
}

I didn't read the full C++0x spec, but is it possible to partial specialize function now?

Upvotes: 0

Yakov Galka
Yakov Galka

Reputation: 72449

You need to unpack in this line too:

bool call_retval_wrapper<bool, Op, Args...>( 

Upvotes: 0

Related Questions