Questions tagged [move-constructor]

In C++11 a move constructor is a special member function similar to a copy constructor, but taking an rvalue-reference parameter.

For more information see Rvalue references and move constructors.

Related tags are and .

344 questions
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…
88
votes
5 answers

How should I deal with mutexes in movable types in C++?

By design, std::mutex is not movable nor copyable. This means that a class A holding a mutex won't receive a default move constructor. How would I make this type A movable in a thread-safe way?
Jack Sabbath
  • 1,140
  • 1
  • 8
  • 12
57
votes
2 answers

Why is derived class move constructible when base class isn't?

Consider the following example: #include #include #include template struct Foo : public Base { using Base::Base; }; struct Bar { Bar(const Bar&) { } Bar(Bar&&) = delete; }; int main() { …
Dmitry
  • 1,123
  • 8
  • 18
38
votes
7 answers

(Why) should a move constructor or move assignment operator clear its argument?

An example move constructor implementation from a C++ course I’m taking looks a bit like this: /// Move constructor Motorcycle::Motorcycle(Motorcycle&& ori) : m_wheels(std::move(ori.m_wheels)), m_speed(ori.m_speed), …
Lynn
  • 8,876
  • 38
  • 66
37
votes
2 answers

Are move constructors produced automatically?

I have a big class holding a lot of STL containers. Will the compiler automatically make a move constructor that will move those containers to the target or I have to make my own?
Dani
  • 27,760
  • 14
  • 76
  • 131
37
votes
4 answers

Should a move constructor take a const or non-const rvalue reference?

In several places I've seen the recommended signatures of copy and move constructors given as: struct T { T(); T(const T& other); T(T&& other); }; Where the copy constructor takes a const reference, and the move constructor takes a…
Ben Hymers
  • 22,821
  • 15
  • 55
  • 79
29
votes
5 answers

Move constructor and const member variables

I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object life time and the destructor finally closes it.…
mazatwork
  • 1,185
  • 12
  • 18
28
votes
4 answers

Move Constructors and Static Arrays

I've been exploring the possibilities of Move Constructors in C++, and I was wondering what are some ways of taking advantage of this feature in an example such as below. Consider this code: template class Foo { public: Foo() { …
Zeenobit
  • 4,506
  • 3
  • 30
  • 45
28
votes
3 answers

Passing/Moving parameters of a constructor in C++0x

If I have a constructor with n parameters such that any argument to that can be an rvalue and lvalue. Is it possible to do support this with move semantics for the rvalues without writing 2^n constructors for each possible rvalue/lvalue combination?
Opt
  • 4,628
  • 3
  • 24
  • 28
28
votes
2 answers

Why is my defaulted move constructor not noexcept?

According to the answer of this question, a default move constructor can be defined as noexcept under certain conditions. For instance, the following class generates a noexcept move constructor: class C {}; According to the answer to this question,…
Pierre
  • 1,647
  • 2
  • 20
  • 33
27
votes
3 answers

When Does Move Constructor get called?

I'm confused about when a move constructor gets called vs a copy constructor. I've read the following sources: Move constructor is not getting called in C++0x Move semantics and rvalue references in C++11 msdn All of these sources are either…
Lauer
  • 419
  • 1
  • 5
  • 10
26
votes
5 answers

Explicit move constructor?

The explicit keyword is recommended for all most constructors which can be called with one argument, except for copy constructors. For copy constructors, it has an use (to forbid implicit copying via function call, return, etc), but it's not what's…
Kos
  • 64,918
  • 23
  • 156
  • 223
25
votes
2 answers

Is move constructor called twice in C++?

Look at this code: class Foo { public: string name; Foo(string n) : name{n} { cout << "CTOR (" << name << ")" << endl; } Foo(Foo&& moved) { cout << "MOVE CTOR (moving " << moved.name << " into -> " << name…
gedamial
  • 1,466
  • 1
  • 14
  • 27
24
votes
4 answers

C++11 rvalue reference calling copy constructor too

I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include #include using namespace std; class TestClass{ public: …
Luke Givens
  • 749
  • 8
  • 15
24
votes
6 answers

Implementing Move Constructor by Calling Move Assignment Operator

The MSDN article, How to: Write a Move Constuctor, has the following recommendation. If you provide both a move constructor and a move assignment operator for your class, you can eliminate redundant code by writing the move constructor to call…
Elliot Hatch
  • 933
  • 1
  • 8
  • 25
1
2 3
22 23