4

Will the following work as expected?:

struct A {};

struct B: public A {
    int x;
    };

void f( B* o ) {
    std::cout << o->x << std::endl;
    }

int main () {
    B b;
    b.x = 5;
    reinterpret_cast<void(*)(A*)>(f)( &b );
    }
3
  • 1
    It most probably depends on the expectations...
    – luk32
    Jun 3, 2016 at 5:36
  • 1
    Is this just academic curiosity, or is there a practical problem you are trying to solve?
    – R Sahu
    Jun 3, 2016 at 5:41
  • 1
    @RSahu: It has actually turned useful for me, but I prefer to keep the example as miminal as possible. Jun 3, 2016 at 8:05

2 Answers 2

6

Its undefined behaviour to use such pointer after cast:

Any pointer to function can be converted to a pointer to a different function type. Calling the function through a pointer to a different function type is undefined, but converting such pointer back to pointer to the original function type yields the pointer to the original function.

From http://en.cppreference.com/w/cpp/language

So the answer to your question is actually positive - you are allowed to cast but nothing more.

You might ask "what is the point of only casting?" - this is usefull when you want to store various functions in single collection.

5

See 5.2.10/6 [expr.reinterpret.cast]:

A function pointer can be explicitly converted to a function pointer of a different type. The effect of calling a function through a pointer to a function type that is not the same as the type used in the definition of the function is undefined.

That said, note as an example that C++ allows you to dereference a null pointer, so maybe allowed is not the right term.
The following command compiles too:

reinterpret_cast<void(*)(A*, int)>(f)( &b, 42 );

It is allowed, as well as the one in the question, no matter if it works as expected or not (it mostly depends on your expectations, as noted by @luk32 in the comments).

The answer to your question would be yes, the cast is allowed, but the invokation of the function through the new pointer leads to an undefined behavior.

9
  • Why surprising? Undefined means, anything thing can happen, not that it won't compile. Basically anything involving reinterpret_cast goes into off-limits. I believe there are similar statements for types too.
    – luk32
    Jun 3, 2016 at 5:52
  • Right, even surprising isn't the right term indeed.
    – skypjack
    Jun 3, 2016 at 5:52
  • I think "allowed" is actually an okay word choice here. The subtext is "is a well-formed C++ program allowed to ... ?". Actually for the reasons you point out I think "allowed" != "happens to compile"
    – Chris Beck
    Jun 3, 2016 at 5:55
  • It is allowed, but when you mix up types there is UB. IMO reinterpret basically turns off type checking, and standard didn't want to deal with it. OPs case will probably work not matter what, but your's is a great example, where you can blow up things, and it's easy to check whether it makes sense or not.
    – luk32
    Jun 3, 2016 at 5:56
  • @ChrisBeck I tried to point out the same concept. The last sentence should sum up it: it is allowed, of course, but do not expect it to work correctly. :-)
    – skypjack
    Jun 3, 2016 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.