c++17 结构化绑定和移动语义
c++17 structured bindings and move semantics
我正在阅读此 https://leanpub.com/cppmove(C++17 - 完整指南第一版 Nicolai M. Josuttisand)并且关于 c++17 结构化绑定和移动语义,它给出了以下示例,其中移动自对象 ms
保持原样:
MyStruct ms = { 42, "Jim" }; // struct has fields {int i, std::string s}
auto&& [v,n] = std::move(ms);
// author says: the structured bindings v and n refer to an anonymous
// entity being an rvalue reference to ms, while ms still holds its value:
std::cout << "ms.s: " << ms.s << '\n'; // prints "Jim"
我已经测试过了,至少在实际运行中,这是真的。但这通常也是它应该如何工作的吗?按照作者之前对结构化绑定的解释,上面应该等同于
MyStruct ms = { 42, "Jim" };
auto&& e = std::move(ms); // anonymous compiler constructed entity "e"
/*aliasname*/ v = e.i; // v and n act as if e was constructed as
// above and v/n assigned to e.i/e.s as here
/*aliasname*/ n = e.s;
std::cout << "ms.s: " << ms.s << '\n'; // prints "Jim"
问题:
所以如果这是一个有效的解释,会不会 auto&& e = std::move(ms)
导致 ms
立即使其内容“移动”/“被盗”/“无效”?因此,在“Jim”上方的两个(等效?)变体中都没有(保证)打印...?
澄清:我知道 std::move 不会移动任何东西,而是从左值到右值的转换,但我认为这暗示至少你不能指望 std::move-ed 对象(此处 ms
)的内容在后续使用中 retained/available
标记为 What is std::move(), and when should it be used?. however, with ~300 upvotes, to that question, this answer:
的欺骗
The first thing to note is that std::move() doesn't actually move
anything. It changes an expression from being an lvalue (such as a
named variable) to being an xvalue. An xvalue tells the compiler:
You can plunder me, move anything I'm holding and use it elsewhere
(since I'm going to be destroyed soon anyway)".
in other words, when you use std::move(x), you're allowing the
compiler to cannibalize x
所以我想混淆是允许的:-P
请为超级傻瓜推荐一本关于 c++17 和移动语义的好书 - 这本书的请求肯定是切合主题的;-)
would not auto&& e = std::move(ms)
cause ms
to immediately have its contents "moved"/"stolen"/"invalidated"?
std::move()
只是准备 ms
被移动,它并没有完全离开它。这就是为什么 e
是对 MyStruct
的 引用 ,而不是实际的 MyStruct
对象。
我正在阅读此 https://leanpub.com/cppmove(C++17 - 完整指南第一版 Nicolai M. Josuttisand)并且关于 c++17 结构化绑定和移动语义,它给出了以下示例,其中移动自对象 ms
保持原样:
MyStruct ms = { 42, "Jim" }; // struct has fields {int i, std::string s}
auto&& [v,n] = std::move(ms);
// author says: the structured bindings v and n refer to an anonymous
// entity being an rvalue reference to ms, while ms still holds its value:
std::cout << "ms.s: " << ms.s << '\n'; // prints "Jim"
我已经测试过了,至少在实际运行中,这是真的。但这通常也是它应该如何工作的吗?按照作者之前对结构化绑定的解释,上面应该等同于
MyStruct ms = { 42, "Jim" };
auto&& e = std::move(ms); // anonymous compiler constructed entity "e"
/*aliasname*/ v = e.i; // v and n act as if e was constructed as
// above and v/n assigned to e.i/e.s as here
/*aliasname*/ n = e.s;
std::cout << "ms.s: " << ms.s << '\n'; // prints "Jim"
问题:
所以如果这是一个有效的解释,会不会 auto&& e = std::move(ms)
导致 ms
立即使其内容“移动”/“被盗”/“无效”?因此,在“Jim”上方的两个(等效?)变体中都没有(保证)打印...?
澄清:我知道 std::move 不会移动任何东西,而是从左值到右值的转换,但我认为这暗示至少你不能指望 std::move-ed 对象(此处 ms
)的内容在后续使用中 retained/available
标记为 What is std::move(), and when should it be used?. however, with ~300 upvotes, to that question, this answer:
的欺骗The first thing to note is that std::move() doesn't actually move anything. It changes an expression from being an lvalue (such as a named variable) to being an xvalue. An xvalue tells the compiler:
You can plunder me, move anything I'm holding and use it elsewhere (since I'm going to be destroyed soon anyway)".
in other words, when you use std::move(x), you're allowing the compiler to cannibalize x
所以我想混淆是允许的:-P
请为超级傻瓜推荐一本关于 c++17 和移动语义的好书 - 这本书的请求肯定是切合主题的;-)
would not
auto&& e = std::move(ms)
causems
to immediately have its contents "moved"/"stolen"/"invalidated"?
std::move()
只是准备 ms
被移动,它并没有完全离开它。这就是为什么 e
是对 MyStruct
的 引用 ,而不是实际的 MyStruct
对象。