News for hackers. Stuff that matters.
Re:Hardening the C++ Standard Library at scale by on_the_train on Saturday November 29, 2025 @02:26PM (#46090051)
But using the deref op is deliberately unsafe, and never used without a check in practice. This would neither pass a review, nor static analysis.
Re:Hardening the C++ Standard Library at scale by canyp on Saturday November 29, 2025 @02:37PM (#46090139)
GP picked the less useful of the two examples. The other one is a use-after-move, which static analysis won't catch beyond trivial cases where the relevant code is inside function scope.

I also agree with them: I am pro-C++ too, but the current standard is a fucking mess. Go and look at modules if you haven't, for example (don't).

Re:Hardening the C++ Standard Library at scale by nly on Saturday November 29, 2025 @06:42PM (#46091937)
No type is safe to use after a move, unless it's documented to put itself in to a well defined state and says as such

You can't magically make all the member functions on std::vector safe after a move for example unless the moved from vector allocates itself a new (empty) buffer, which kills the performance benefits.

It's all by design.

Re:Hardening the C++ Standard Library at scale by surajrmal on Sunday November 30, 2025 @12:18AM (#46094032)
I believe it's actually the opposite. You're supposed to be able to reuse objects that were moved from unless otherwise documented, although it may require reinitializing it explicitly. A moved from vector is valid to reuse. Although the standard doesn't specify what state it will be in, all major standard library implementations return it to the default constructed state which doesn't require an allocation.
Re:Hardening the C++ Standard Library at scale by nly on Sunday November 30, 2025 @05:23AM (#46095480)
Read what I wrote again. I said all member functions.

Reusing of a moved from object only requires assignment and destruction to be well behaved.

The std library containers give you extra guarantees (a moved from object is effectively the same as a default constructed one), but the _language_ imposes no such requirements on your types

It's perfectly allowed by the language for the .size() member of your own vector type to return a random value after it's been moved from because you wanted to save 1 CPU instruction somewhere.

Re:Hardening the C++ Standard Library at scale by surajrmal on Sunday November 30, 2025 @12:39PM (#46098689)
I understand what you're saying but no major compiler does anything nefarious. They default initialize it. A lot of code depends on std::optional that was moved from returning false for is_valid for instance and no one would break that even if it's not guaranteed.
Re:Hardening the C++ Standard Library at scale by simonask on Sunday November 30, 2025 @04:34AM (#46095195)
This is false. Moved-from is a "valid but unspecified" state in C++, so perfectly safe, but each type must decide what to do. At the minimum, the destructor must be able to run, because it will be invoked, meaning that the obvious choice is also the only sensible one: letting moved-from be equivalent to the "empty" state.

An empty std::vector does not require that any buffer is allocated. It just has a null data pointer.

Re:Hardening the C++ Standard Library at scale by nly on Sunday November 30, 2025 @06:00AM (#46095649)
See my sibling comment regarding vector.

It's not true that the only sensible choice for a moved-from object be equivalent to the defaulted constructed one.

If your move constructor doesn't exist then the copy constructor gets called under the language rules, so the sensible default is actually a copy.

Everything else is an optimisation that has a trade-off

A conforming implementation of std::list, for example, can have a default constructor and a move constructor that both allocate a sentinel node on the heap, which is why none of the constructors are noexcept.

If you don't allocate a sentinel on the heap, then moving std::list can invalidate iterators (which is what GNU stdlibc++'s implementation chooses).

It's a trade off.

Re:Hardening the C++ Standard Library at scale by simonask on Sunday November 30, 2025 @06:55AM (#46095905)
I did not say “default-constructed”, because that’s a whole other can of worms.

But yes, the implication of C++ move semantics is that every movable object must also define an “empty” (moved-from) state, so you cannot have something like a never-null unique ptr.

Specifically, it is not allowed for the moved-from object to be inconsistent or to say “using it in any way is UB”, because its destructor will run.

Re:Hardening the C++ Standard Library at scale by IshKebab on Saturday November 29, 2025 @03:24PM (#46090443)
> never used without a check in practice

Ho ho ho good one.

Re:Hardening the C++ Standard Library at scale by mohinder on Saturday November 29, 2025 @02:59PM (#46090296)
> This would neither pass a review, nor static analysis

I beg to differ. Humans are fallible. Static analysis of C++ cannot catch all cases and humans will often accept a change that passes the analyses.

Re:Hardening the C++ Standard Library at scale by einpoklum on Saturday November 29, 2025 @03:29PM (#46090489)
> Static analysis of C++ cannot catch all cases

You're ignoring how static analysis can be made to err on the side of safety rather than promiscuity.

Specifically, for optional dereferencing, static analysis can be made to disallow it unless it can prove the safety.