move-semantics

Move Semantics with unique_ptr

[亡魂溺海] 提交于 2020-12-29 05:55:19
问题 I am using Visual Studio 2012 Update 2 and am having trouble trying to understand why std::vector is trying to use the copy constructor of unique_ptr. I have looked at similar issues and most are related to not having an explicit move constructor and/or operator. If I change the member variable to a string, I can verify that the move constructor is called; however, trying to use the unique_ptr results in the compilation error: error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

▼魔方 西西 提交于 2020-12-27 07:43:49
问题 In C++11 we can transfer the ownership of an object to another unique_ptr using std::move() . After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr. std::unique_ptr<int> p1(new int(42)); std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership What are the situations where this will be useful as it is transferring the ownership to another unique_ptr ? 回答1: The following situations involve transferring ownership from one unique

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

邮差的信 提交于 2020-12-27 07:42:47
问题 In C++11 we can transfer the ownership of an object to another unique_ptr using std::move() . After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr. std::unique_ptr<int> p1(new int(42)); std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership What are the situations where this will be useful as it is transferring the ownership to another unique_ptr ? 回答1: The following situations involve transferring ownership from one unique

Return std::tuple and move semantics / copy elision

天大地大妈咪最大 提交于 2020-11-24 16:35:17
问题 I have the following factory function: auto factory() -> std::tuple<bool, std::vector<int>> { std::vector<int> vec; vec.push_back(1); vec.push_back(2); return { true, vec }; } auto [b, vec] = factory(); In the return statement is vec considered an xvalue or prvalue and therefore moved or copy elided? My guess is no, because the compiler, when list-initializing the std::tuple in the return statement, still doesn't know that vec is going to be destroyed. So maybe an explicit std::move is

Exact correspondence between r-value references and pointers?

青春壹個敷衍的年華 提交于 2020-08-05 13:31:43
问题 This is a general question about symmetry between pointer and reference types in the C++ language. Is this table of correspondence meaningful in C++ (C++11 and beyond)? +-----------+----------------+ | Reference | Pointer | |-----------|----------------| | T& | T* const | | T const& | T const* const | | T&& | ???* | +-----------+----------------+ and if some, what would correspond to ???* ?. (Any additional rows are missing?) ( T&& is for a concrete type T , not a deduced type. See

Why does C++ move semantics leave the source constructed?

泄露秘密 提交于 2020-07-31 09:40:42
问题 In C++11, "move semantics" was introduced, implemented via the two special members: move constructor and move assignment. Both of these operations leave the moved-from object constructed. Wouldn't it have been better to leave the source in a destructed state? Isn't the only thing you can do with a moved-from object is destruct it anyway? 回答1: In the "universe of move operations" there are four possibilities: target source is is left ----------------------------------------------------------