Search code examples
c++overloadingpointer-to-membertypeidtypeinfo

c++ typeid on class member operator() overloads


When do the following in gcc 4.8.2,

class A
{
public:
   void operator()(int);
   void operator()(const std::string&) {}
};

std::cout << typeid(&A::operator()).name() << std::endl;

It gives errors:

error: address of overloaded function with no contextual type information.

It works for other class member operators, such as operator==. Is that a syntactic error in the typeid() call for A::operator()?

EDITS: sorry, my bad. The problem occurs when there are multiple overloaded operator()'s.


Solution

  • You could use static_cast to pick up what you want from overloads.

    static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));

    e.g.

    std::cout << typeid(static_cast<void(A::*)(int)>(&A::operator())).name() << std::endl;
    

    LIVE