Implementing Move Constructor by Calling Move Assignment Operator

[…] will the compiler be able to optimize away the extra initializations? In almost all cases: yes. Should I always write my move constructors by calling the move assignment operator? Yes, just implement it via move assignment operator, except in the cases where you measured that it leads to suboptimal performance. Today’s optimizer do an … Read more

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

You take each one by value, like this: struct foo { foo(std::string s, bar b, qux q) : mS(std::move(s)), mB(std::move(b)), mQ(std::move(q)) {} std::string mS; bar mB; qux mQ; }; The initialization of the function parameters by the argument will either be a copy-constructor or move-constructor. From there, you just move the function parameter values into … Read more

Explicit move constructor?

An explicit move constructors can affect compatibility with e.g. Standard algorithms. For instance, std::swap<T> requires that T be MoveConstructible. In turn, MoveConstructible is specified in terms of an expression, namely T u = rv; (where rv is an rvalue of type T). If there is neither a non-explicit copy constructor nor a non-explicit move constructor … Read more

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

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

They don’t need to be cleaned. The designer of the class decided it would be a good idea to leave the moved-from object zero initialized. Note that a situation where is does make sense is for objects managing resources that get released in the destructor. For instance, a pointer to dynamically allocated memory. Leaving the … Read more

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

Because: A defaulted move constructor that is defined as deleted is ignored by overload resolution. ([class.copy]/11) Bar‘s move constructor is explicitly deleted, so Bar cannot be moved. But Foo<Bar>‘s move constructor is implicitly deleted after being implicitly declared as defaulted, due to the fact that the Bar member cannot be moved. Therefore Foo<Bar> can be … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)