News for hackers. Stuff that matters.
Re:I am sick and tired of hearing tech companies complain ab... by amluto on Wednesday May 19, 2021 @12:27AM (#27204583)
And footguns that an experienced person might hit. One family of such footguns is:

    const auto &foo = something(args);
    …
    other_thing(foo);
Depending on exactly what you’re doing and what type something(args) returns, this can be entirely correct and idiomatic. In other circumstances it accesses a reference after its lifetime.

One can debate whether the const belongs there and how many &’s to use, but, unless something has changed dramatically in the last couple years (I’ve been focused on things that aren’t C++), there is no answer that is universally safe and efficient.

Re:I am sick and tired of hearing tech companies complain ab... by rightbyte on Wednesday May 19, 2021 @06:41PM (#27215446)
The temporary's reference is valid until the temporary is destroyed by falling out of scope, swapped etc. Or have I missed something?
Re:I am sick and tired of hearing tech companies complain ab... by amluto on Friday May 21, 2021 @10:50AM (#27234743)
The reference itself is indeed valid, in the sense of existing, until it falls out of scope. But the object it references may not be, so using the reference can be UB. Consider an std::vector v with length known to be at least 1:

    const auto &ref = v[0];
    v.push_back(42);
    cout << ref;
That works fine until push_back reallocates out of place, at which point you may start to notice that it’s actually UB.

Rust will not permit this problematic usage. GC languages tend not to offer this pattern — you can reference a boxed vector element with different semantics than the above C++ code, and you mostly can’t reference an unboxed element. C++ lets you do things like this but has little ability to statically verify correctness.

(Doing the above maneuver with a vector is a bit silly: it saves typing, and indexing a vector is extremely fast. With a map, though, indexing is not so fast, and keeping an iterator or a reference around may be a big performance win. At least if you keep an iterator around, dynamic checkers have a better chance of noticing errors.)

Re:I am sick and tired of hearing tech companies complain ab... by esrauch on Thursday May 20, 2021 @09:34AM (#27220917)
I think the point is just that unlike Rust there's no compiler enforcement that you only use refs in their lifetimes: you can easily accidentally cause a use after free.

Some times it can be pretty indirect: eg if the ref ends up pointing into some std container then it might need invalidated by making an otherwise unrelated change to the container.

Re:I am sick and tired of hearing tech companies complain ab... by pas on Wednesday May 19, 2021 @01:01AM (#27204763)
Could you please explain this? I remember something about rvalues but it was as ages ago apparently :)
Re:I am sick and tired of hearing tech companies complain ab... by swader999 on Wednesday May 19, 2021 @01:20AM (#27204847)
First time I've ever heard the term 'footgun'
Re:I am sick and tired of hearing tech companies complain ab... by josephg on Wednesday May 19, 2021 @01:35AM (#27204952)
It’s a reference to a decades-old quote from Bjarne Stroustrup, C++’s creator:

“In C++ it’s harder to shoot yourself in the foot, but when you do, you blow off your whole leg.”