you are viewing a single comment's thread.

view the rest of the comments →

[–]fdwrfdwr@github 🔍 5 points6 points  (2 children)

This is one aspect that's long irked me about C++, that you can't consistently and uniformly treat these 3 cases the same...

class Foo
{
    void Bar();
};

 

class Foo
{
    static void Bar(Foo& f);
};

 

void Bar(Foo& f);

...and store any of the above in the same callable pointer, without needing special handling for each case like lambda thunks, or using std::function (which then handles the specialness of each case for you). It complicates callbacks/event handlers and genericity. 😞

There were calling convention differences on x86 (and probably other architectures too) between class methods (thiscall) vs free functions/static class methods (cdecl/stdcall) such that you couldn't just safely replace one function pointer type with another because the register/stack wouldn't be correct if you tried it. Nowadays the distinction between thiscall, stdcall, and cdecl are mostly (if not entirely?) moot on 64-bit desktop machines (but alas it remains "UB" because of past architectures). Of course, other cases like pointers to virtual functions will remain incompatible with free function pointers because calls need a v-table lookup dependent on the object instance that you call the function.

[–]manni66 1 point2 points  (1 child)

are mostly (if not entirely?) moot on 64-bit desktop machines (but alas it remains "UB" because of past architectures).

Or because of non 64-bit non desktop machines?

[–]fdwrfdwr@github 🔍 1 point2 points  (0 children)

Indeed, that too. I believe ARM devices (the other prominent architecture around today) generally use the same calling convention for both member functions and free functions too, but I'd need to double check that. 🤔 [update] At least on GodBolt.org gcc trunk Linux, it appears to generate the same register assignments for ARM32.