r/cpp • u/meetingcpp Meeting C++ | C++ Evangelist • May 21 '26
Exploring ref qualifiers in C++
https://meetingcpp.com/blog/items/Exploring-ref-qualifiers-in-Cpp.html
41
Upvotes
r/cpp • u/meetingcpp Meeting C++ | C++ Evangelist • May 21 '26
3
u/meetingcpp Meeting C++ | C++ Evangelist May 21 '26
One thing I still was lacking when I wrote and researched this was actual usage numbers.
So I've launched some surveys on LinkedIn/Twitter/Mastodon, and for now it seems to fall into a range of 44 - 80% of folks using/knowing them.
9
u/kalreshka May 22 '26
constcan also be combined with&&. In template code you can sometimes (probably unintentionally) end up with const-qualified r-value reference. Explicitly deleting such overloads can be helpful if such use-case is a mistake you want to catch.4
u/bwmat May 21 '26
I used these recently in some member accessors to avoid potential dangling references when updating an API from returning a pointer into an internal data member to returning an object by value (also added * and - > operator overloads to allow some legacy code using the API to continue working w/ only a recompile)
3
u/fdwr fdwr@github 🔍 May 22 '26
I wish in the context of
f(...)&thatthiswas treated as a reference, so I could saythis.x(rather thanthis->x). That would make it more consistent with "deducing this". Oh well.