Subh_b
Subh_b

Reputation: 13

"invalid use of incomplete type" for const function pointer type as template argument

Consider the following simplified code for a functor wrapper:

#include <iostream>
#include <utility> // std::forward

template <class F>
struct return_type;

template <class R, class C, class... A>
struct return_type <R (C::*)(A...)> {
  typedef R type;
};

// ----------------------

template <class FunctorType, typename FunctionPointerType>
class functor_wrapper {
public:
    FunctorType* f;
    FunctionPointerType p;
    functor_wrapper (FunctionPointerType pp) : p(pp) { f = new FunctorType; }

    template <class... A>
    typename return_type<FunctionPointerType>::type  operator() (A && ... args) {
        return ( (f->*p) (std::forward<A>(args)...) );
    }
};

// ----------------------

class my_less {
public:
    bool non_const_mem (const int& x, const int& y) {return x<y;}
    bool const_mem (const int& x, const int& y) const {return x<y;}
};

// ----------------------

int main(int argc, char *argv[])
{
    // functor_wrapper <my_less, bool (my_less::*)(const int&, const int&)>  foo(&my_less::non_const_mem); // OK!!
    functor_wrapper <my_less, bool (my_less::*)(const int&, const int&) const>  foo(&my_less::const_mem); // ERROR!!
                                                                    //  ^^^^
    std::cout << "is 2<5? " << (foo(2,5)?"yes":"no") << std::endl;
}

In the declaration of 'foo', if I use the constant member function I get the compilation error "invalid use of incomplete type ‘struct return_type<bool (my_less::*)(const int&, const int&)const>’". However, if it is the non-constant member function, it compiles and runs just fine. I don't understand where the "incomplete type" is in this code when the member function is of constant type, and what I can possibly do to make it work for the constant member functions?

I am using gcc version 4.8.4.

Upvotes: 1

Views: 302

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477040

You are missing a suitable qualified template specialization, like this one:

template <class R, class C, class... A>
struct return_type <R (C::*)(A...) const> {
    typedef R type;             // ^^^^^
};

Upvotes: 2

Related Questions