logoalt Hacker News

cocotoyesterday at 10:28 PM2 repliesview on HN

Why are some examples full of errors? The `set_vec` method for instance does not bind the reference, you can't change the reference itself... so the code would simply copy the vector and there would be no dangling reference... And `B` is missing a constructor since the default constructor would be ill-formed (you can't default initialize a reference).

Anyway the article is quite approachable, do not take my criticism to shy away from writing!


Replies

tialaramextoday at 12:53 AM

I strongly encourage people writing textual examples in 2026 to use Compiler Explorer.

https://cpp.godbolt.org/

Matt Godbolt's tool lets your reader play with your examples and learn more about what's going on. As a bonus, if it doesn't compile and work in Compiler Explorer now you know early before you hit "publish". It's the same reason you should run a spellchecker, raweht thun jstu hope forr th bess

pixelesqueyesterday at 10:57 PM

Yeah, that example's totally wrong, as you say, the std::vector<int> would get copied by value, so there'd be no issue at all.