3 ms·
C++ uses name mangling to give names to non-virtual methods. They are no different than a C function that takes a pointer, except they may use a different calli
by webstrand 4y ago
C++ uses name mangling to give names to non-virtual methods. They are no different than a C function that takes a pointer, except they may use a different calling convention.
Even if the method is virtual, C++ compilers make an effort to call methods directly without indirection through a vtable wherever possible.
- tialaramex 4y agoThe interesting thing is that you can't call the "function that takes a pointer". Accessing it as a method is the only thing which works. A long term ambition for C++ and more specifically for Bjarne has been UFCS, Universal Function Call Syntax, which a few other languages have. But C++ can't do this today.
- cbsmith 4y agoYeah, UFCS remains illusive, but tantalizingly close. It's pretty easy to make ``` struct foo { void bar(); } bar(foo* i) { assert(i != nullptr); i->bar(); } ``` work, but there's enough syntactic complexity in the language that it isn't as easy as it should be.
- deleted 4y ago[deleted]
- kllrnohj 4y agostd::mem_fn will effectively give you the underlying function that takes a pointer. Strictly speaking mem_fn is defined to generate the wrapper function, but in practice the optimizer is just going to strip that away and call the name-mangled function directly. Example: https://godbolt.org/z/6Y1raxMce https://godbolt.org/z/6Y1raxMce
- Matheus28 4y agoYou can just use member function pointers without the wrapper. The syntax for calling them is a bit uglier though.
- kllrnohj 4y agoYup, but then the call syntax is different so it fails to achieve the "Universal Function Call Syntax" that OP was asking for which std::mem_fn provides.
- gavinray 4y agoWhoa, this is really cool, thanks for posting