vendredi 26 juin 2015

Template specialization and Function overloading in C++

I am trying to build a static wrapper function that will be able to call member and and non-member function pointers.

What I have come up with is as follows -

template <typename Derived>
struct CallAny {
  template<typename F> using ReturnType = typename std::function<typename std::remove_pointer<F>::type>::result_type;

  template<typename F, bool IsMember, typename... Args>
  static ReturnType<F> function_wrapper(Derived* obj, F func, Args... args);

  template<typename F, typename... Args>
  static ReturnType<F> function_wrapper<F, true, Args...>(Derived *obj, F func, Args... args) {
    return obj->*func(args...);
  }

  template<typename F, typename... Args>
  static ReturnType<F> function_wrapper<F, false, Args...>(Derived *obj, F func, Args... args) {
    return func(args...);
  }

};

struct CallAnyDerived : public CallAny<CallAnyDerived> {}; 

Here F is a function pointer type that has to be called with arguments given in Args.... F might be a C-style function pointer or a member of class Derived which inherits from CallAny.

When I try to compile this code, I get an error saying -

XXXXXXXXXXX: error: function template partial specialization ‘fucntion_wrapper<F, true, Args ...>’ is not allowed
   static ReturnType<F> function_wrapper<F, true, Args...>(Derived *obj, F func, Args... args) {
                                                                                             ^
XXXXXXXXXXX: error: function template partial specialization ‘fucntion_wrapper<F, false, Args ...>’ is not allowed
   static ReturnType<F> function_wrapper<F, false, Args...>(Derived *obj, F func, Args... args) {
                                                                                              ^

How do I fix this?

Aucun commentaire:

Enregistrer un commentaire