Questions tagged [move-assignment-operator]

66 questions
153
votes
3 answers

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, but I don't remember, and also can't find a reputable…
145
votes
6 answers

Move assignment operator and `if (this != &rhs)`

In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don't screw things up: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment …
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
16
votes
2 answers

using swap to implement move assignment

Something occurred to me which I think it completely reasonable, but I'd like people's opinion on it in case I'm just completely missing something. So firstly, my understanding of T& operator=(T&& rhs) is that we don't care what the contents of rhs…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
13
votes
2 answers

What is the rationale for self-assignment-unsafe move assignment operators in the standard library?

The standard library policy about move assignment is that the implementation is allowed to assume that self-assignment will never happen; this seems to me a really bad idea, given that: the "regular" ("copy") assignment contract in C++ has always…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
10
votes
2 answers

Is move assignment via destruct+move construct safe?

Here's a very easy way to define move assignment for most any class with a move constructor: class Foo { public: Foo(Foo&& foo); // you still have to write this one Foo& operator=(Foo&& foo) { if (this != &foo) { …
9
votes
3 answers

free(): double free detected in tcache 2 in C++

Firstly, I really checked if there is a question already been asked but I could not find any. Error message should not deceive you my situation is a bit different I guess or I am just missing something. While I was dealing with a toy C++ code I…
8
votes
2 answers

Is it legal to implement assignment operators as "destroy + construct"?

I frequently need to implement C++ wrappers for "raw" resource handles, like file handles, Win32 OS handles and similar. When doing this, I also need to implement move operators, since the default compiler-generated ones will not clear the…
8
votes
3 answers

msvc /permissive- std::string overloaded operator '=' is ambiguous

It compiles with /permissive but fails with /permissive-. What is not conforming and how to fix it? Why it's fine in (2) but fails in (4)(3)? If I remove operator long it also fine. How to fix it without changing call site (3,4)? #include…
7
votes
1 answer

Why in C++11 or C++14 does the compiler implicitly delete the copy constructor when I declare a move assignment operator?

I wanted to create a list data structure with an iterator class in it. Everything works well but when I declare a move assignment operator the program doesn't compile if it's using the C++14 or C++11 standards, but works fine in C++17,…
Kanony
  • 509
  • 2
  • 12
6
votes
3 answers

Questions about the move assignment operator

Imagine the following class that manages a resource (my question is only about the move assignment operator): struct A { std::size_t s; int* p; A(std::size_t s) : s(s), p(new int[s]){} ~A(){delete [] p;} A(A const& other) :…
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
6
votes
1 answer

std::vector move assignment vs move construction: why is the state of 'other' not consistent?

For move construction: After the move, other is guaranteed to be empty(). 1 For move assignment, the oft-quoted: other is in a valid but unspecified state afterwards. 2 Why is the state of other different in these two cases?
sleep
  • 4,855
  • 5
  • 34
  • 51
6
votes
1 answer

Why std::sort construct objects?

I created the following class to understand the behavior of std::sort: class X { public: X(int i) : i_(i) { } X(X&& rhs) noexcept : i_(std::move(rhs.i_)) { mc_++; } X& operator=(X&& rhs) noexcept { i_ = std::move(rhs.i_); ao_++; return…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
6
votes
2 answers

Assigning make_unique to shared_ptr

I (erroneously) had the following assignment in my program: std::shared_ptr m_program; // in class m_program = std::make_unique(); // in method When I found this, I first wondered why this even compiles. It turns out the…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
6
votes
2 answers

Double move on same object is copying from left to right?

I am just beginner in move operation in c++11, so playing with it. But found something which i am not able to understand. #include using namespace std; class A{ public: A(){cout << "default ctor" << endl;} A(const…
Rupesh Yadav.
  • 894
  • 2
  • 10
  • 24
6
votes
3 answers

How to ensure the move constructor is used

The code below gives the error: use of deleted function ‘constexpr B::B(const B&)’ now, I know this happens because the copy constructor is (intentionally) implicitly deleted by specifying a move constructor, and that copying the vector causes the…
Oebele
  • 581
  • 1
  • 4
  • 17
1
2 3 4 5