FAQ Database Discussion Community


Difference between the move assignment operator and move constructor?

c++,c++11,constructor,move-semantics
For some time this has been confusing me. And I've not been able to find a satisfactory answer thus far. The question is simple. When does a move assignment operator get called, and when does a move constructor operator get called? The code examples on cppreference.com yield the following interesting...

How to add elements to vector without invoking copy-constructor?

c++,c++11,vector,copy-constructor,move-semantics
For some reason my temporary local object is always copy-constructed/destroyed when added to a vector which is causing problems due to nested std::reference_wrapper which are getting invalid because of the copy-construction and destruction afterwards (std::reference_wrapper targets are inside the object which gets destroyed -> so they are invalid in the...

What happens to an object instance after applying std::move

c++,std,move-semantics
I am trying to understand how std::move and rvalues work in C++ 11. I keep seeing the similar example in tutorials like this: Suppose we have this class : class Class { public: Class(Class &&b) { a = b.a; } int *a; } int main() { Class object1(5); Class object2(std::move(object1));...

Why does std::move take a universal reference?

c++,c++11,move-semantics,rvalue,universal-reference
The implementation of std::move basically looks like this: template<typename T> typename std::remove_reference<T>::type&& move(T&& t) { return static_cast<typename std::remove_reference<T>::type&&>(t); } Note that the parameter of std::move is a universal reference (also known as a forwarding reference, but we're not forwarding here). That is, you can std::move both lvalues and rvalues: std::string...

Is the contents of a pointer to a unique_ptr's contents valid after the unique_ptr is moved?

c++,pointers,move-semantics,unique-ptr
I've been led to understand that calling a member function on the contents of a moved-from std::unique_ptr is undefined behaviour. My question is: if I call .get() on a unique_ptr and then move it, will the original .get() pointer continue to point to the contents of the original unique pointer?...

Object's container field is empty after move and erase

c++,vector,move-semantics,erase
I have two vectors and I want to move an object from one to the other, before I move it I make a pointer to the object. But when I erase the index in first vector then the container within the object is empty if I access it through the...

Usage of std::swap() inside move assignment should cause endless recursion (and causes), but it is an example from Stroustrup's book

c++,c++11,recursion,move-semantics
I'm trying to get the deep knowledge about how should I write my copy and move constructors and assignment operators. In Bjarne Stroustrup's "The C++ Programming Language - 2013" I see the following example of move constructor and move assignment: template<class T, class A> vector_base<T,A>::vector_base(vector_base&& a) : alloc{a.alloc}, elem{a.elem}, space{a.space},...

Fallback to copy constructor not working?

c++,c++11,copy-constructor,c++14,move-semantics
I thought that when I delete the move constructor in B then the following code will still compile fine since it should still take the copy constructor to construct B objects. Why does the compiler now complain. Without the =delete it did not and called the copy constructor anyway since...

How would move semantics improve “my way”?

c++,c++11,move-semantics,copy-and-swap
Background I read the following answers earlier today, and it felt like relearning C++, litterally. What is move semantics? What is the copy-and-swap idiom? Then I wondered if I should change my "ways" to use these exciting features; the main concerns I have are for code efficiency and clarity (former...

Releasing memory with std::move()?

c++,c++11,move-semantics
Says I have a class with a standard container: class Library{ std::vector<Book> books; public: void putOnFire(){ books.clear(); } }; The usual way to clear a container is "clear", however most code is not "STL compliant" so many containers (by third parties) may not have a "clear" method. however, If they...

Is it safe to reuse a std container after std::move? [duplicate]

c++,c++11,std,move-semantics
This question already has an answer here: Reusing a moved container? 3 answers If I have a std::list, for example, and I use std::move() to move its information into a std::map, is it safe to reuse the list by populating it with more data so that I can add...

How is a lambda moved?

c++,c++11,lambda,c++14,move-semantics
I don't understand how a lambda is moved. Consider the following code: #include <iostream> #include <utility> #include <string> struct Foo // non-copyable type { Foo() = default; Foo(const Foo&) = delete; // cannot copy Foo(Foo&&) = default; // can move }; auto lambda = [p = Foo()](){ std::string x{"lambda"}; std::cout...

if i have T&& temp = std::move(other); then use this on a function that accepts T by value

c++11,move,move-semantics
so let's say i have a following function: void foo(std::string strParam) // pass-by-value { // function-body } so strParam of foo(string) will either be created via copy (if arg was lvalue) or move (if arg was rvalue). as everybody knows, foo("blah"); // rvalue; so string move constructor invoked for strParam....

Understanding move semantics for std::array

c++,c++11,move-semantics
I am trying to understand move semantics, so I did the following test: #include <iostream> #include <array> using namespace std; void tryToMove(array<double,3> && v) { array<double,3> v_ = std::move(v); std::cout << v_[0] << " " << v_[1] << " " << v_[2] <<'\n'; } int main () { array<double,3> v{1,2,3};...

How to implement move constructor for large size non-pointer member?

c++,c++11,move-semantics,move-constructor
On this website there is an example of simple class with a move constructor. How would a move constructor of a similar class look: class MemoryPage { std::vector<char> buff; public: explicit MemoryPage(int sz=512) { for(size_t i=0;i<sz;i++) buff.push_back(i); } }; would it be: MemoryPage::MemoryPage(MemoryPage &&other) { this->buff = std::move(other.buff); } ?...

Move semantics in the presence of virtual bases

c++11,move-semantics,virtual-inheritance,diamond-problem
Consider the following program: #include <iostream> #include <ostream> #include <string> #include <utility> using namespace std; struct Name { string s; Name(string s) : s(move(s)) { } }; struct A : virtual Name { A(string s) : Name(move(s)) { } }; struct B : virtual Name { B(string s) : Name(move(s))...

Does erasing a shared_ptr from a set deletes the managed object

c++,shared-ptr,move-semantics
In the following code, I expect the memory that pointer tr points, to be destroyed. However even though I verified sp1, points to the same address with tr, and clearing the set causes the trial object sp kept to be destroyed, tr still points to the same address and the...

C++11: Does a move operation change the address?

c++,pointers,c++11,move-semantics,memory-address
Let's say that I have a main class SomeManager for keeping track of instances of another class SomeClass. When SomeClass is constructed it calls a method of SomeManager passing a pointer to it self. Then SomeManager takes that pointer and pushes it into a vector. The destructor of SomeClass calls...

How to improve std::vector parameter passing (move semantics?)

c++,c++11,move-semantics
It seems that I cannot completely understand move semantics: I want to fill an std::vector (member of a class) from an external function. Currently, I have something like: void fillVector(MyClass & myclass) { std::vector<int> vec; /* Filling vec */ // ... myclass.setVector(vec); } class MyClass { public: setVector(const std::vector<int> &...

C++ why does empty set::emplace() inserts an element into a set of pointers?

c++,c++11,set,move-semantics
Consider the following code: struct A{}; int main() { std::set<A*> aset; aset.emplace(); std::cout << aset.size() << std::endl; //prints "1" return 0; } DEMO Why does the empty emplace() adds an element to the set of pointers?...

Why doesn't this RAII move-only type properly emulate `std::unique_ptr`?

c++,c++11,move-semantics,unique-ptr
I took the code from this question and edited it to produce a segfault by explicitly calling the destructor of one of the move-constructed objects: using namespace std; struct Foo { Foo() { s = new char[100]; cout << "Constructor called!" << endl; } Foo(const Foo& f) = delete; Foo(Foo&&...

move class data members (C++)

c++,c++11,move-semantics
I want to know if I'm doing this right. I have a class that holds some data: class Foo { // ... Type a_; Type b_; Type c_; }; And a different class that does something else, but is constructed using class Foo. So, I reckon declare a ctor like...

std::push_heap and std::pop_heap with MoveConstructible objects

c++,c++11,stl,heap,move-semantics
I want to maintain a heap where the payloads are MoveConstructible (because they hold a std::unique_ptr inside.) Although the documentation suggests that the object must be MoveAssignable and MoveConstructible, doing so throws an error both in GCC and Clang. Sample Code I don't use an std::unique_ptr yet, but just disable...

For what is the move constructor and move assignment constructor in Unique Pointers needed?

c++,c++11,smart-pointers,move-semantics,unique-ptr
I've got a simplified example of a unique_ptr. I wonder for what the move constructor and move assignment operator in the unique pointer is needed? If I understand move constructors (and passing rvalues) right, these two lines of code should result the same. UniquePointer<T> a(new T); UniquePointer<T> a(UniquePointer<T>(new T)); And...

VC2013 move operator doesn't recursively perform move

c++,visual-studio-2013,move-semantics
The following code doesn't compile in VS2013. #include <memory> #include <vector> struct Struct { std::unique_ptr<int> data; }; int main() { std::vector<Struct> vec; vec.emplace_back(); vec.emplace_back(); vec.front() = std::move(vec.back()); return 0; } I get the following error: error C2280: attempting to reference a deleted function It seems like VS compiler is trying...

move semantics and unique_ptr

c++,move-semantics,unique-ptr
How would you perform a move operation on a class that uses unique_ptr? Wouldn't setting the unique_ptr to null cause deletion of the data? If I perform a copy through a list initializer of the unique_ptr like so, would the data be preserved or deleted? template<typename T, typename A =...

call rvalue-constructor from an lvalue-constructor

c++,c++11,move-semantics,method-overloading,rvalue-reference
I'd like to provide a constructor definition only once – for both lvalue and rvalue. class A; class B { B(A const& a): B(A(a)) {} B(A&&); }; Is B::B(A&&) guaranteed to be called by B::B(A const&)?...

Seg fault on GCC4.6 when trying to move a large bitset, is this a compiler bug?

c++,gcc,segmentation-fault,undefined-behavior,move-semantics
I've experienced a problem with a crash due to return an rvalue of a bitset when the bitset is large. Is this a compiler bug or have I mistakenly done something that caused undefined behaviour? The code below crashes on GCC 4.6.3 with the -std=c++0x flag set. #include <bitset> //...

Class with explicit destructor and std::unique_ptr<> member can't be used in std::vector<>?

c++,c++11,move-semantics
This code #include <memory> #include <vector> class Foo { public: ~Foo() { } std::unique_ptr<int> bar; }; int main() { std::vector<Foo> foos; foos.emplace_back(); } yields the following error message in g++: In file included from /usr/include/c++/4.8/memory:64:0, from main.cpp:1: /usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Foo; _Args...

Abstract classes and move semantics

c++,c++11,move-semantics
According to “Rule Of Five” when I declare one of: copy or move operation or destructor I must write all of them, because the compiler doesn't generate them (some of them) for me. But if my class (A) derives from an abstract class with a virtual destructor, does this mean...

C++11: shortest way to explicitly copy a value to use as an rvalue reference

c++,c++11,copy-constructor,move-semantics,generic-programming
I have a function class A { <has copy and move constructors > }; void f(A &&a) { ... } I want to call the function f with a variable x, but I want to run f on a copy of x, not x itself. Why can't I do something...

Move constructor for std::string from char*

c++,string,c++11,move-semantics
I have a function f returning a char*. The function documentation says: The user must delete returned string I want to construct a std::string from it. The trivial things to do is: char* cstring = f(); std::string s(cstring); delete cstring; Is it possibile to do it better using C++ features?...