As for the code duplication, that's partially alleviated by a language feature introduced in C++23:
deducing `this` https://youtu.be/eD-ceG-oByA?si=L5XIOpsQLYVT-laP&t=1045
and another part of it is addressed by adhering to the "rule of zero":
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...
struct Foo
{
int Bar(const Baz& b1, const Baz& b2) const;
// ...
};
The compiler can't reasonably do anything helpful with it _because_ of the above rules. Once you peek behind that curtain, it kind of crumbles.> As for the code duplication, that's partially alleviated by a language feature introduced in C++23:
Agreed about deducing this, it's a bit awkward but solves a real pain point.
Well, you can delegate work to a function with `__restrict` on its parameters, and then the compiler _can_ help you. Though you would probably need to check that the _restrict_ is valid. Still, classes should probably not do the performance heavy-lifting.
... but then again on the other hand, `__restrict` is a compiler intrinsic, not really part of C++. That's a gaping hole right there if you ask me.
Func( foo, foo);
I don't want foo to be loaded twice.Theres also the `const` on the function itself. If I do:
struct Foo {
int GetVal() const {
return Val;
}
int Val
};
void Bar(const Foo& f){
int val1 = f.GetVal();
int val2 = f.GetVal();
assert(val1 == val2);
}
This is not guaranteed for a variety of reasons. This means that there are many optimisations that are just unavailable because of const, and there are guarantees that on first glance you expect to be true, but arent.I understand why, we don't need to go into it, but its a mess that const doesn't actually mean constant.
It just means you've opted into coloured functions [0]. It's not quite as painful as async functions in js, but all the same arguments apply.
[0] https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... t