Hacker News new | ask | show | jobs
by nickysielicki 714 days ago
The whole thing is wrong. Don’t put const references in your structs. Use std::reference_wrapper if you must.

Edit: this response is a bit dismissive but honestly my main beef with this article is that its conclusion is just straight up wrong. Do not write your own constructors, do follow the rule of 5/3/0, and if you find yourself needing to hold a const reference, you should look out for whether you’re passing in an rval temporary… none of this is really scary.

3 comments

Rule of 5/3/0 is about destructors, move and copy constructors. Apart from the last example, the article mainly talks about quirks with the standard constructor, if you can call it that.
main takeaway of the article according to the author, quoting:

> In my humble opinion, here’s the key takeaway: just write your own fucking constructors! You see all that nonsense? Almost completely avoidable if you had just written your own fucking constructors. Don’t let the compiler figure it out for you. You’re the one in control here. Or is it that you think you’re being cute? You just added six instances of undefined behaviour to your company’s codebase, and now twenty Russian hackers are fighting to pwn your app first. Are you stupid? What’s the matter with you? What were you thinking? God.

The problem with C++ and the danger with an article like this is someone might actually follow this advice, instead of eg: the core guidelines.

Every other example is a violation of the core guidelines in some form or another. There is no other problem.

Don't know why I got sniped into this on a friday night, but here's clang-tidy with only cppcoreguideline checks enabled, against every example:

First example: https://godbolt.org/z/898rorEqG

Second example: https://godbolt.org/z/K6aceesG9

Third example: https://godbolt.org/z/KcbqzMzdK

and I can't get the fourth example to compile without fixing the problems.

> Edit: this response is a bit dismissive but honestly my main beef with this article is that its conclusion is just straight up wrong.

That's my take as well. The blogger clearly went way out of his way to find something to whine about while purposely ignoring a few of the most basic principles and guidelines.

In the meantime, everyone who ever went through a basic tutorial just casually steps over these artificial scenarios.

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.

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.