Live data from Hacker News

C++ std::move doesn't move anything: A deep dive into Value Categories

0xghost.dev

51–60 of 220 posts

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#51
post #32

About 28 years ago, I figured out that I’m just not smart enough to use C++. There are so many foot guns and so much rampant complexity that I can’t keep it all straight. I crave simplicity and it always felt like C++ craved the opposite.

Same. I’ve read all the books. Written all these things at least a few times. It’s just not doable post C++11.

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#52

Earlier quoted context omitted.

It is absolutely knowable statically if ownership will be taken. It's not necessarily very easy to do so, but the decision is 100% up to the compiler, as part of overload resolution and optimization choices (like the NRVO analysis that the article mentions). Since ownership is an inherently static concept, it doesn't even make sense to think about "runtime ownership".

I don't understand the downvoted here. Either the compiler emits the code to call a move constructor or it doesn't.

Static analysis is about proving whether the code emitted by a compiler is actually called at runtime. It's not simply about the presence of that code.

Code can be emitted but never executed.

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#53
post #49
post #48

Earlier quoted context omitted.

TFA explains how std::move is tricky to use and this is not a feature reserved for library writers

Of course it is not reserved for library writers - nothing is. But it is not a feature that application writers should worry about overmuch.

std::move is definitely for there for optimizing application code and is often used there. another silly thing you often see is people allocating something with a big sizeof on the stack and then std::moving it to the heap, as if it saves the copying

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#54
Before move semantics the HeavyObject problem was solved in most cases by specializing std::swap for each container.

The design lesson I draw from this is that pursing a 100% general solution to a real problem is often worse than accepting a crude solution which covers the most important cases.

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#56
post #32

About 28 years ago, I figured out that I’m just not smart enough to use C++. There are so many foot guns and so much rampant complexity that I can’t keep it all straight. I crave simplicity and it always felt like C++ craved the opposite.

Systems programming in the large is hard, owning the category for decades harder still.

Even languages that have tried to fast-follow and disrupt C++ end up looking a lot like C++. There is an irreducible complexity.

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#57

Earlier quoted context omitted.

The only thing that is statically known here is that you’re wrong. The function I posted only moves its parameter half the time, at random. You may want to treat it as moved-from either way, but factually that’s just not what is happening.

This is like trying to defend that you can't statically know the result of 1 + 2 because: void foo() { std::random_device rdev {}; auto dist = std::uniform_int_distribution (0, 1); if (dist(rdev)) { int res = 1 + 2; } } I can tell you for sure that the result of 1 + 2 will be 3.

> This is like trying to defend that you can't statically know the result of 1 + 2

It is completely unlike that. tsimionescu is asserting that they can always know statically whether `foo` will move its parameter. The function I provided is a counter-example to that assertion.

Of course the branch body always moves, that's what it's there for. That has no bearing on the argument.

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#58
post #26

Earlier quoted context omitted.

Std move doesn’t move ownership. It simply casts into something that could have its ownership taken. Whether or not that actually happens is impossible to identify statically and the value after ownership is consumed is unspecified - sometimes it’s UB to access the value again, sometimes it’s not.

After moving a value, it needs to remain in a "valid but unspecified state". How do you mean accessing a valid object is UB?

"Validity" is an extremely low bar in C++, it just means operations with no preconditions are legal, which in the most general case may be limited to destruction (because non-destructive moves means destruction must always be possible).

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#59
post #22

> So the standard library plays it safe: if your move constructor might throw (because you didn’t mark it noexcept), containers just copy everything instead. That “optimization” you thought you were getting? It’s not happening. This is a bit of a footgun and clang-tidy has a check for it: performance-noexcept-move-constructor. However, I don't think it's enabled by default!

Most sensible Compiler flags aren't enabled by default... I keep a list of arguments for gcc to make things better, but even then you'll also wanna use a static analysis tool like clang-tidy

Would you mind sharing your list?

Re: C++ std::move doesn't move anything: A deep dive into Value Categories

#60
> [std::move silently copies const values, because] If something is const, you can’t move from it by definition.

Whoever wrote that definition should have a thing or two to learn from Rust. Different language I know, but it proves that it wasn't needed to cause so much confussion and collectively so much time and performance lost.

Also, who writes rules like that and ends the day satisfied with the result? It seems unlikely to feel content with leaving huge footguns and being happy to push the Publish button. I'd rather not ship the feature than doing a half-assed work at it. Comparing attitudes on language development and additions, it makes me appreciate more the way it's done for the Go lang, even though it also has its warts and all.

Post reply on HN