2 ms·
(Context: https://learnmoderncpp.com/2022/12/07/applications-of-c23s-deducing-this/ https://learnmoderncpp.com/2022/12/07/applications-of-c23s-d...) While the
by vitus 3y ago
(Context: https://learnmoderncpp.com/2022/12/07/applications-of-c23s-deducing-this/ https://learnmoderncpp.com/2022/12/07/applications-of-c23s-d...)
While the article describes why you'd want this abomination, honestly, I'd rather opt for the copy-pasted code
T& at(size_t x, size_t y) { return data.at(y * X + x); }
const T& at(size_t x, size_t y) const { return data.at(y * X + x); }
as something that's actually human-legible while also providing const-correctness. If the implementation is actually complicated enough, then sure, use the const_cast variation, but hide it away in a .cc file. This feels like a contrived example to show off new language features.
That said, I'd also rather use std::mdspan [0] if I had C++23 available, and not implement my own Matrix struct by hand.
[0] https://en.cppreference.com/w/cpp/container/mdspan https://en.cppreference.com/w/cpp/container/mdspan
- protomolecule 3y agoIt isn't contrived, just incomplete. You'd need to copy-paste it four times -- there is &, &const, && and && const overloads (the fourth isn't making much sense, but still).