Questions tagged [move-assignment-operator]
55 questions
135
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
- 70,115
- 19
- 169
- 239
135
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…
oompahloompah
- 8,321
- 18
- 59
- 89
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
- 115,256
- 16
- 181
- 279
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…
OwnageIsMagic
- 680
- 8
- 23
8
votes
1 answer
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
- 80,654
- 26
- 169
- 231
8
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) { …
Bjarke H. Roune
- 3,337
- 2
- 17
- 24
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
- 462
- 12
6
votes
1 answer
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…
Fredrik Orderud
- 111
- 9
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
- 18,256
- 1
- 39
- 74
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
- 521
- 1
- 3
- 15
6
votes
2 answers
Why defining a destructor deletes the implicitly defined move assignment operator?
What is the rationale behind the C++ Standard Committee choice of deleting the implicitly defined move assignment operator when a customized destructor is defined?
nyarlathotep108
- 4,663
- 1
- 16
- 46
5
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
- 11,461
- 15
- 67
- 116
4
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
- 46,179
- 14
- 109
- 158
4
votes
3 answers
Compiler won't use copy assignment instead move?
I have a class where the move assignment is explicit deleted, since the object should not be moveable. But if i assign to an instance of this class using RVO the compiler gives me the error:
main.cpp:12:16: note: candidate function has been…
user1810087
- 4,541
- 1
- 34
- 66
4
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.
- 866
- 1
- 9
- 23