Live data from Hacker News

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

0xghost.dev

21–30 of 220 posts

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

#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!

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

#23
post #7

Maybe std::make_movable would have been a slightly better name, but it's so much simpler to write std::move.

thanks to the incredible advances in terms of developer tooling over the last 50 years (i.e. tab-autocompletion) there should be no difference in writing those two.

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

#26
post #2

I always understood move as moving ownership, so it's not a misnomer. > std::move is like putting a sign on your object “I’m done with this, you can take its stuff.” Which exactly is moving ownership.

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?

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

#27
post #7

Maybe std::make_movable would have been a slightly better name, but it's so much simpler to write std::move.

Split the difference with std::moveable().

Also signals it doesn't actually move, while remaining just as fast to type.

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

#29
post #18

Should have be called give(). But naming things correctly is hard, and the C++ committee is known to do a lot of things incorrectly

That has about the same issue: like std::move it doesn't really explain that the receiver decides.

std::offer

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

#30
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?

>After moving a value, it needs to remain in a "valid but unspecified state".

No, it doesn't.

The standard library requires that for its classes, but not the language.

"Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state."[0]

[0] https://timsong-cpp.github.io/cppwp/n4950/lib.types.movedfro...

Post reply on HN