Member function call in decltype

HighCommander4 picture HighCommander4 · Feb 28, 2011 · Viewed 8.1k times · Source

The following code:

struct A
{
    int f(int);
    auto g(int x) -> decltype(f(x));
};

Fails to compile with the error:

error: cannot call member function 'int B::f(int)' without object

If I change it to:

struct A
{
    int f(int);
    auto g(int x) -> decltype(this->f(x));
};

I get another error:

error: invalid use of 'this' at top level

What is wrong with either of these? I am using gcc 4.6

Answer

TonyK picture TonyK · Mar 1, 2011

Here are the magic words:

struct A
{
    int f(int);
    auto g(int x) -> decltype((((A*)0) ->* &A::f)(x)) ;
};

Edit I see from Mikael Persson's answer that this is how it's done in boost.