News for hackers. Stuff that matters.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by cbsmith on Monday November 28, 2022 @12:21PM (#33776389)
I mean, if you change the name to "BufferPool::get_record" and you use an implicit this pointer instead of your "pool" argument, you have a method. It seems like you're fighting the paradigm more than you need to.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by gavinray on Monday November 28, 2022 @12:37PM (#33776606)
You're not wrong but for some reason this style is easier for me to work with

I know logically/semantically they're equivalent. The v-table function generated if the method is moved inside the class/struct winds up being identical.

Dunno what it is, some kind of familiarity bias with the way the code is laid out I guess, not coming from OOP languages

Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by thelopa on Monday November 28, 2022 @12:45PM (#33776763)
Note: it wouldn’t generate a vtable entry unless the method was declared virtual.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by gavinray on Monday November 28, 2022 @12:49PM (#33776826)
Ahh, what do you call the method table thing for non-virtual classes?
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by cornstalks on Monday November 28, 2022 @12:55PM (#33776926)
There is no table if no ‘virtual’ is involved.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by adrian_b on Monday November 28, 2022 @02:49PM (#33778603)
To be more clear, when no "virtual" method is declared, then no table is generated because none is needed.

In this case, the addresses of all the methods are known at compile time and the compiler knows which to choose when anyone is invoked in the source code, based on the type of the object and on the types of the method arguments.

When there is at least one "virtual" method and you have a pointer to an object, the type of the object cannot be known at compile-time, so the compiler cannot determine which method to invoke.

That is why the compiler needs to create a table with pointers to the virtual methods for each class with such methods, and each object of those classes must include a pointer to the corresponding vtable, so that the correct method to invoke can be determined at run time (from the index of the virtual method).

Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by webstrand on Monday November 28, 2022 @01:53PM (#33777835)
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.

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.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by kllrnohj on Thursday December 01, 2022 @04:49PM (#33823370)
Yup, 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.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by gavinray on Tuesday November 29, 2022 @03:45PM (#33793045)
Whoa, this is really cool, thanks for posting
Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by protomolecule on Monday November 28, 2022 @03:14PM (#33778898)
Only classes with one or more virtual functions have virtual method table. Objects of such classes are called polymorphic objects and they (and only they) have hidden pointer to virtual method table.

https://en.cppreference.com/w/cpp/language/object#Polymorphi...

Re:Const vs. constexpr vs. consteval vs. constinit in C++20 by cbsmith on Monday November 28, 2022 @12:55PM (#33776911)
> The v-table function generated if the method is moved inside the class/struct winds up being identical.

Unless you add some additional semantics/keywords, it's not going to create a vtable.

> Dunno what it is, some kind of familiarity bias with the way the code is laid out I guess, not coming from OOP languages.

I'm sure that's what it is.