move-semantics

Deleting move constructor and constructing object from rvalue

妖精的绣舞 提交于 2020-01-15 08:08:40
问题 I'm trying to understand Item 17 from "Effective Modern C++" about special member function generation so I was trying some examples and am trying to reason about some behavior. In the book it says: ..that when I refer to a move operation move-constructing or move-assigning a data member or base class, there is no guarantee that a move will actually take place. “Memberwise moves” are, in reality, more like memberwise move requests, because types that aren’t move-enabled (i.e., that offer no

Conversion to `const Y` not applicable for `R&&` on clang

大城市里の小女人 提交于 2020-01-14 09:43:10
问题 The following code compiles fine with g++ (GCC) 4.7.1 20120721 , but fails with a recently build clang version 3.2 (trunk) . struct Y {}; struct X { operator const Y() const { return Y(); } }; void f(Y&& y) {} int main() { f(X()); return 0; } Changing the conversion operator to operator Y() const is sufficient to make the code compile on both compilers. Which compiler is actually standard compliant in this case? What does the standard actually say about this? The verbatim error as requested:

Move constructor/operator=

时间秒杀一切 提交于 2020-01-13 07:21:10
问题 I'm trying to learn about new feature of C++ namely move constructor and assignment X::operator=(X&&) and I found interesting example but the only thing I quite not even understand but more dissagree is one line in move ctor and assignment operator (marked in the code below): MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { std::cout << "In MemoryBlock(MemoryBlock&&). length = " << other._length << ". Moving resource." << std::endl; // Copy the data pointer and its length from

Should std::array have move constructor?

白昼怎懂夜的黑 提交于 2020-01-12 12:15:48
问题 Moving can't be implemented efficiently (O(1)) on std::array, so why does it have move constructor ? 回答1: std::array has a compiler generated move constructor, which allows all the elements of one instance to be moved into another. This is handy if the elements are efficiently moveable or if they are only movable: #include <array> #include <iostream> struct Foo { Foo()=default; Foo(Foo&&) { std::cout << "Foo(Foo&&)\n"; } Foo& operator=(Foo&&) { std::cout << "operator=(Foo&&)\n"; return *this;

Move semantics and perfect forwarding difference

橙三吉。 提交于 2020-01-10 14:15:13
问题 I already got what move semantics is from this question: What are move semantics? But I still do not get what perfect forwarding is in relation to move semantics. Can someone explain in simple english and with a simple example what perfect forwarding means? 回答1: Plain English-only attempt The problem is probably too complex to be accurately described by plain English sentences, but one could think of perfect forwarding as a way to move temporary values passed to a function to another one as

Why can't I move the std::unique_ptr inside lambda in C++14?

眉间皱痕 提交于 2020-01-10 11:58:10
问题 I want to pass a raw pointer inside lambda, but I don't want it to be leaked, if the lambda isn't invoked. It looks like this: void Clean(std::unique_ptr<int>&& list); void f(int* list) { thread_pool.Push([list = std::unique_ptr<int>(list) ] { Clean(std::move(list)); // <-- here is an error. }); } I get an error in Clang 3.7.0: error: binding of reference to type 'unique_ptr<[2 * ...]>' to a value of type 'unique_ptr<[2 * ...]>' drops qualifiers But I don't see any qualifiers at the first

Does moving leave the object in a usable state?

十年热恋 提交于 2020-01-08 16:34:31
问题 Say I have two vectors and I move one unto the other, v1 = std::move(v2) ; will v2 still be in a usable state after this? 回答1: From n3290, 17.6.5.15 Moved-from state of library types [lib.types.movedfrom] Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. Since the state is valid, this means you

Does moving leave the object in a usable state?

China☆狼群 提交于 2020-01-08 16:34:06
问题 Say I have two vectors and I move one unto the other, v1 = std::move(v2) ; will v2 still be in a usable state after this? 回答1: From n3290, 17.6.5.15 Moved-from state of library types [lib.types.movedfrom] Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. Since the state is valid, this means you

Splitting range into sub-ranges

僤鯓⒐⒋嵵緔 提交于 2020-01-06 07:08:03
问题 I have a container std::vector and I would like to efficiently split it into sub-ranges with x items in each. The original container is not needed so the items should be moved and not copied into the sub-ranges. I've managed to do the splitting using copying, however I'm unsure how to do it with move assignments? range.insert(range.end(), new_items.begin(), new_items.end()); while(range.size() >= x) { sub_ranges.push_back(std::vector<int>(range.begin(), range.begin() + x)); range = std:

Map of lists of move-only type won't compile

落爺英雄遲暮 提交于 2020-01-05 02:36:10
问题 I am trying to use a std::map< Key, std::list< std::unique_ptr< T > > > . Am I missing some fundamental reason why this is not possible ? I've reduced the code giving the error to the following: // A simple move-only type struct moveonly { moveonly(moveonly const&) = delete; moveonly& operator= (moveonly const&) = delete; moveonly() = default; moveonly(moveonly&&) = default; moveonly& operator= (moveonly&&) = default; ~moveonly() noexcept {} }; typedef std::list< moveonly > list_t; typedef