Search code examples
c++templatesgeneric-programmingpointer-to-member

c++ compiling error when using member functions as a template parameters


I'm trying to pass a member function pointer as a template parameter. Here is the code:

template <typename Ret, typename T, Ret(T::*mptr)()>
Handle<Value> get_value (Local<String> name, const AccessorInfo& info)
{
    ...
}

template <typename Ret, typename T>
void mbind (const char* name, Ret (T::*mptr)())
{
    ....
    objectTemplate->SetAccessor (String::NewSymbol (name),get_value<Ret,T,mptr>);
}

And this is the error I'm getting:

wrapper.h:184:5: error: ‘mptr’ is not a valid template argument for type ‘int (Cell::*)()’
wrapper.h:184:5: error: it must be a pointer-to-member of the form `&X::Y'
...

As far as I know pointers to member functions are valid template parameters. I don't understand what's wrong with the previous code. The compiler I'm using is g++ 4.5.2 under Ubuntu.

Thanks in advance.

UPDATE:

It's seems the code should be wrong as mptr is a runtime variable. On the other hand, the previous excerpt of code compiles:

http://ideone.com/cv8pq

so...is it correct? does it depend on the compiler?


Solution

  • mptr is a runtime variable - you cannot give it as a template parameter. Check http://ideone.com/CIL4C .

    Edit

    Strange thing is http://ideone.com/cv8pq where something similar to your code successfully compiles and works.