Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by tialaramex on Monday November 28, 2022 @03:50PM
(#33779422)
The 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.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by cbsmith on Monday November 28, 2022 @03:58PM
(#33779524)
Yeah, 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.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by kllrnohj on Monday November 28, 2022 @07:36PM
(#33782047)
It's a 1-liner with std::mem_fn
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by cbsmith on Monday November 28, 2022 @10:08PM
(#33783234)
Indeed.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by kllrnohj on Monday November 28, 2022 @07:40PM
(#33782090)
std::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
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by Matheus28 on Tuesday November 29, 2022 @03:10AM
(#33784898)
You can just use member function pointers without the wrapper. The syntax for calling them is a bit uglier though.
Even if the method is virtual, C++ compilers make an effort to call methods directly without indirection through a vtable wherever possible.