Hacker News new | ask | show | jobs
by forrestthewoods 710 days ago
I’ve never used std::reference_wrapper in my life. Nor have I seen it used in any of the numerous C++ code bases I’ve worked in. Although I’m sure it’s used in deep, gnarly template BS.

Your statement may be correct! But it’s certainly not common knowledge in my experience.

3 comments

My stance is just based on cpp core guidelines:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...

std::reference_wrapper still can’t save you from yourself, but it’s better than violating the first link and ending up in this limbo that OP is talking about.

See also: https://youtu.be/YxSg_Gzm-VQ (about 3:30 in)

That link says nothing about std::reference_wrapper ?
I’m just saying, nothing up my sleeve, no arcane templatelord bullshit, this is isocpp: don’t use references in your structs because it’s subtly broken. It doesn’t mention reference wrappers but that’s the escape hatch.
I use them now and then -- and I never touch templates. It's just a reference you can reassign. Or, in other words, truly a pointer that can't be null. :D (And that you can actually use in collections that require reassignable types.)
If you want to use assignment operators with references that is what it does.

Nothing else.