Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'&' : illegal operation on bound member function expression [duplicate]

Tags:

c++

This works when I try from a single cpp file with a main function,

 sprintf(smem_options ,
     "#transcode{vcodec=RV24}:smem{"
     "video-prerender-callback=%lld,"
     "no-time-sync},"
    , (long long int)(intptr_t)(void*)&cbVideoPrerender
);

How do I pass function arguments to sprintf within a class?

sprintf(smem_options ,
     "#transcode{vcodec=RV24}:smem{"
     "video-prerender-callback=%lld,"
     "no-time-sync},"
    , (long long int)(intptr_t)(void*)&cbVideoPrerender
);

The error message I get is: error C2276: '&' : illegal operation on bound member function expression

like image 593
Jerry Avatar asked Jan 20 '15 16:01

Jerry


2 Answers

Assuming cbVideoPrerenderer is a member function in the second example, you need to say &Foo::cbVideoPrerenderer where Foo is the class it is a member of.

But that will only be valid if it is a static member function. Non-static member functions are not like normal functions, and when you form a pointer-to-member-function with the &Foo::bar syntax the thing you get back cannot be converted to a void* (it is typically something twice as large as a pointer, as it contains information about the object type).

like image 79
Jonathan Wakely Avatar answered Oct 06 '22 18:10

Jonathan Wakely


What you're trying to do is conditionally supported behavior in C++11, and illegal in earlier versions, in both cases. You can't reliably convert a pointer to a function (member or otherwise) to a void*. (I've worked on systems where a pointer to a function was 32 bits, but a void* only 16.)

In practice, most compilers will (illegally, in pre-C++11) ignore the error for non-member functions. Posix requires that function pointers and data pointers be compatible, and they are under Windows as well. (Today: only of the systems where they weren't for me was an early Unix.) As for pointers to members: a pointer to a static member has a type compatible to a pointer to a function (and so will work in practice, if the compiler allows it), but a pointer to a non-static member has a completely different type, usually with a different size, and a different representation. About the only way you can reliably output one is as a series of byte values: put the address in a properly typed variable, take the address of that variable, convert it to unsigned char const*, then use "%02x" to output each byte.

But the real question is why you want to do this. There is nothing that you can reliably do with the value you output, regardlessly of how you output it.

like image 45
James Kanze Avatar answered Oct 06 '22 18:10

James Kanze