0

I am trying to cast a method to a void pointer so that I can use it as a callback method.

void* pVoidedFunc = &testmethod;

But I get the error:

error: invalid conversion from int (*)() to void*

The method is:

static int testmethod()
{
   return 0;
}

How do I cast methods into void pointers?

Holtorf
  • 1,451
  • 4
  • 21
  • 41
  • 4
    Why would you want to cast methods into void pointers? – Brian Walker Sep 12 '14 at 20:05
  • possible duplicate of [Casting between void \* and a pointer to member function](http://stackoverflow.com/questions/1307278/casting-between-void-and-a-pointer-to-member-function) – IdeaHat Sep 12 '14 at 20:06
  • If this is for a callback method then passing the pointer to the callback method should have the correct signature and not use a generic void*. – Brian Walker Sep 12 '14 at 20:10
  • What call are you trying to feed the void * too? – Surt Sep 12 '14 at 20:15
  • possible duplicate of [Function pointers casting in C++](http://stackoverflow.com/questions/1096341/function-pointers-casting-in-c) – Niall Sep 12 '14 at 20:18
  • 1
    Do you really need a void * for your function pointer? That's a really poor design. If the callback is a function that takes no parameters and returns int, it should be declared as such. Surely the code calling the callback will be casting the void* to a pointer to function taking no arguments and returning int. Ignoring all the preaching, you'll need a (void *) cast to fix the error. – KC-NH Sep 12 '14 at 20:23
  • Did you have a look at [boost::mem_fn](http://www.boost.org/doc/libs/1_56_0/libs/bind/mem_fn.html) or [boost::bind](http://www.boost.org/doc/libs/1_56_0/libs/bind/bind.html) ? – Carlo Wood Sep 12 '14 at 20:25
  • @Brian Walker I discovered what I was doing wrong farther in the code about 5 minutes after I asked the question. You are right, I did not actually need to cast the function to a void pointer. – Holtorf Sep 12 '14 at 20:26
  • @KC-NH The real callback method was much more complicated. This was just a toy example because if this one line function was not going to work, then there was no way the actual method was going to work. – Holtorf Sep 12 '14 at 20:28

1 Answers1

1

The language doesn't allow auto conversion of a pointer to a function to a void pointer.

Here's what the C++ Draft Standard (N3337) says about pointer conversion (emphasis mine):

4.10 Pointer conversions

2 An rvalue of type “pointer to cv T,” where T is an object type, can be converted to an rvalue of type “pointer to cv void.” The result of converting a “pointer to cv T” to a “pointer to cv void” points to the start of the storage location where the object of type T resides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject).

Functions are not objects. This is stated in:

1.8 The C+ + object model

1 The constructs in a C + + program create, destroy, refer to, access, and manipulate objects. An object is a region of storage. [Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. ]

Object type is defined as:

3.9 Types

9 An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270