move-semantics

Is the capacity required to be preserved when moving a std::vector?

a 夏天 提交于 2019-12-03 22:39:23
Consider the following code: std::vector vec; vec.reserve(500); size_t cap = vec.capacity(); std::vector newVec = std::move(vec); assert(cap == newVec.capacity()); In pretty much any implementation you run across, this will work. I don't care about what implementations do. I want to know what the standard requires . Will the moved-to vector have the same capacity as the original? Or will the assert trigger? Looking at the standard, it appears that nothing is required from the move constructor, however as @amaurea says, it would completely defeat the purpose of move semantics if the move

Should std::array have move constructor?

痴心易碎 提交于 2019-12-03 22:29:13
Moving can't be implemented efficiently (O(1)) on std::array, so why does it have move constructor ? 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; } }; int main() { std::array<Foo, 10> a; std::array<Foo, 10> b = std::move(a); } So I would say std::array

C++11 constructor argument: std::move and value or std::forward and rvalue reference

家住魔仙堡 提交于 2019-12-03 21:46:24
Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward<Y>(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} }; The two variants differ in functionality. The following statements work for the second one–but not for the first one: Y y; X x(y); If you are looking for the same functionality, the two variants should look as follows: struct X { Y data_; explicit X(const Y& data) : data_(data) { } explicit X(Y&& data) : data_(std::move(data)) { } }; struct X { Y data_; explicit X(Y data) : data_(std::move(data))

Move constructor and initialization list

谁都会走 提交于 2019-12-03 15:59:00
I want to implement move constructors (no copy constructor) for a certain type that needs to be a value type in a boost::unordered_map . Let's call this type Composite . Composite has the following signature: struct Base { Base(..stuff, no default ctor) : initialization list {} Base(Base&& other) : initialization list {} } struct Composite { Base member; Composite(..stuff, no default ctor) : member(...) {} Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base } I want to write this so boost::unordered_map< Key , Composite > does

How to trick boost::asio to allow move-only handlers

感情迁移 提交于 2019-12-03 15:24:42
问题 In a RPC communication protocol, after the invocation of a method I'm sending "done" messages back to the caller. Since the methods are invoked in a concurrent fashion, the buffer containing the response (a std::string ) needs to be protected by a mutex. What I'm trying to achieve is the following: void connection::send_response() { // block until previous response is sent std::unique_lock<std::mutex> locker(response_mutex_); // prepare response response_ = "foo"; // send response back to

Why isn't move construction used when initiating a vector from initializer list (via implicit constructor)

筅森魡賤 提交于 2019-12-03 14:43:23
To demo move semantics, I wrote the following example code, with an implicit constructor from int. struct C { int i_=0; C() {} C(int i) : i_( i ) {} C( const C& other) :i_(other.i_) { std::cout << "A copy construction was made." << i_<<std::endl; } C& operator=( const C& other) { i_= other.i_ ; std::cout << "A copy assign was made."<< i_<<std::endl; return *this; } C( C&& other ) noexcept :i_( std::move(other.i_)) { std::cout << "A move construction was made." << i_ << std::endl; } C& operator=( C&& other ) noexcept { i_ = std::move(other.i_); std::cout << "A move assign was made." << i_ <<

Move semantics for a conversion operator

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:48:15
What's the syntax for a movable conversion operator? I have a wrapper that wraps around obj , which has an obj conversion operator: class wrap { public: operator obj() { ... } private: obj data_; }; How can I find out whether data_ should be copied or moved? The syntax for that would be something like this: class wrap { public: operator obj() const & { ... } //Copy from me. operator obj() && { ... } //Move from me. private: obj data_; }; The first version will be called when the second version cannot be called (ie: the wrap instance being converted is not a temporary or there is no explicit

Do I use std::forward or std::move here?

做~自己de王妃 提交于 2019-12-03 12:25:39
问题 Let's say I have: template<class T> struct NodeBase { T value; NodeBase(T &&value) : value(value) { } }; and I inherit from it: template<class T> struct Node : public NodeBase<T> { Node(T &&value) : NodeBase( WHAT_GOES_HERE (value)) { } }; Should WHAT_GOES_HERE be std::move or std::forward<T> ? Why? 回答1: Since in the implementation of the constructor of Node<T> it is unknown whether T is a plain type (i.e. not a reference), or a reference, std::forward<T>(value) is suitable. std::forward<T>

Insert map entry by r-value moving of mapped_type

我的未来我决定 提交于 2019-12-03 11:23:54
I have a map with a (fairly) simple key-type and a complex mapped-type, like so: map<string, vector<string>> myMap; If I have a vector<string> in hand, is it possible to insert an entry into the map which copies the key but moves the mapped-value? That is, is there some way to do: string key = "Key"; vector<string> mapped; for (int i = 0; i < 1000; ++i) mapped.push_back("Some dynamic string"); // Insert by moving mapped; I know I'm done with it myMap.insert(make_pair(key, move(mapped))); // This seems to move key too You are looking for std::map::emplace : myMap.emplace(key, move(mapped));

Make a class non-copyable *and* non-movable

我只是一个虾纸丫 提交于 2019-12-03 10:55:17
Before C++11, I could use this to make a class non-copyable: private: MyClass(const MyClass&); MyClass& operator=(const MyClass&); With C++11, I can do it like this instead: MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&) = delete; When using the class with the deleted copy and assignment, is there a chance that a default move operator is generated? And the class is not exactly copied, but moved (which is sort of similar) after all? So, do I have to do this to prevent default move construction and assignmnent: MyClass(MyClass&&) = delete; MyClass& operator=(MyClass&&) =