When Does Move Constructor get called?

A move constructor is called: when an object initializer is std::move(something) when an object initializer is std::forward<T>(something) and T is not an lvalue reference type (useful in template programming for “perfect forwarding”) when an object initializer is a temporary and the compiler doesn’t eliminate the copy/move entirely when returning a function-local class object by value … Read more

C++11 rvalue reference calling copy constructor too

Put noexcept on your move constructor: TestClass(TestClass&& other) noexcept { Elaboration: I was going to give this one Pierre, but unfortunately the cppreference source is only approximately correct. In C++03 vector<T>::push_back(T) has the “strong exception guarantee”. That means that if the push_back throws an exception, the vector is left in the same state it had … Read more

Why doesn’t C++ move construct rvalue references by default? [duplicate]

with your design: void doWork(Widget && param) { Widget store1 = param; // automatically move param Widget store2 = param; // boom Widget store_last = param; // boom } with current design: void doWork(Widget && param) { Widget store1 = param; // ok, copy Widget store2 = param; // ok, copy Widget store_last = std::move(param); … Read more

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

Let’s start with a bit of code: class A { using MutexType = std::mutex; using ReadLock = std::unique_lock<MutexType>; using WriteLock = std::unique_lock<MutexType>; mutable MutexType mut_; std::string field1_; std::string field2_; public: … I’ve put some rather suggestive type aliases in there that we won’t really take advantage of in C++11, but become much more useful in … Read more

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

In the following, “auto-generated” means “implicitly declared as defaulted, but not defined as deleted”. There are situations where the special member functions are declared, but defined as deleted. The default constructor is auto-generated if there is no user-declared constructor (ยง12.1/5). The copy constructor is auto-generated if there is no user-declared move constructor or move assignment … Read more