4 ms·
I feel that modern c++ is a game of how many times you can fit `const` in a single declaration. I usually have at least three for the simplest of functions.
by harporoeder 3y ago
I feel that modern c++ is a game of how many times you can fit `const` in a single declaration. I usually have at least three for the simplest of functions.
- maccard 3y agoConst in C++ is probably one of the biggest frustrations I have with the language. It's an API promise, but suffers from all the downfalls of "what colour are your functions". Combine that with aliasing, const_cast, mutable, you're left with a pinky promise, with code duplication that I honestly wonder sometimes if we're better off without
- einpoklum 3y agoThen, in typical C++ form, I'll tell you that you are probably "doing it wrong". Just avoid const_cast and mutable in your application code altogether. Keep the const safety and the const will keep you safe :-) . Well, somewhat, granted. There is aliasing; and the fact that `restrict` is not formally part of the C++ is a pain point for sure. 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 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#Rc-zero https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...
- maccard 3y agoThe problem is that if I write 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.
- einpoklum 3y ago> The compiler can't reasonably do anything helpful with it _because_ of the above rules. 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.
- 3836293648 3y agoWell, it's a compiler intrinsic to expose a part of C that's missing in C++. It's not like it's something GCC or clang came up with. It's like `_Bool` and `_Generic`
- zabzonk 3y agowhat do you want that function to do? and why does const not let you?
- 3836293648 3y agoYou want to avoid reloading data all the time, but you can't because of aliasing
- zabzonk 3y agothat doesn't really answer my question.
- maccard 3y agoIt does - when I call 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-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... t