views:

46

answers:

2

Why is this code resulting in a compiler error?

#include <iostream>
#include <algorithm>

using namespace std;

class X
{
    public:
        void Print(int x)
        {
            cout << x << endl;
        }
};

int main()
{
    X x;
    mem_fun_ref<void, X, int>(&X::Print) p;
};

Error main.cpp:18: error: expected ; before p

+2  A: 

mem_fun_ref is a function template, so it does not name a type.

mem_fun_ref<void, X, int>(&X::Print) is a function call that returns a value, so it makes no sense that there is a p following it.

The return value of that function call is a mem_fun1_ref_t<void, X, int>, in case you were looking for that.

sth
+2  A: 

Did you intend to write

mem_fun1_ref_t<void, X, int> p(&X::Print);
           ^^^^             ^^^

instead? mem_fun_ref is not a class template, but a function template.

usta