Sadeq
Sadeq

Reputation: 8043

Unified Call Syntax and Function Pointers

A bit of background for the unified call proposal

Does Unified Call Syntax allow C++ programmers to easily cast a member function pointer to a non-member function pointer and vise versa?

In other words, does it allow to call a member function via a non-member function pointer by sending the class's instance as the first parameter?

struct X
{
    void member() { }
};

X x;

void (*fp)(X*) = &X::member; // ?
fp(&x);                      // ?

Upvotes: 1

Views: 369

Answers (1)

Yam Marcovic
Yam Marcovic

Reputation: 8141

No, that can't ever work, because a pointer-to-member-function can't be represented by a simple function pointer. A pointer-to-member-function must be able to handle virtual calls on the object it's applied on, and so it must contain (or trigger the generation of) code to dereference a vptr when necessary. I.e., it's not a regular pointer you can just call into.

Upvotes: 4

Related Questions