move-constructor

C++0x move constructor gotcha

The C++ move constructor and move assignment operator seem like really positive things. And they can be used in situations where the copy constructor makes no sense because they don't require duplicating resources being pointed at. But there are cases where they will bite you if you aren't careful. And this is especially relevant as I...

Why does this C++0x program generates unexpected output?

This program: test_header.hpp #include <boost/signal.hpp> #include <utility> class Sensor; class Recorder : public ::boost::signals::trackable { public: explicit Recorder(int id) : id_(id) {} // Cannot be copied Recorder(const Recorder &) = delete; Recorder &operator =(const Recorder &) = delete; // But can be moved...

Move constructor and assignment operator: why no default for derived classes?

Why there is default move constructor or assignment operator not created for derived classes? To demonstrate what I mean; having this setup code: #include <utility> struct A { A () { } A (A&&) { throw 0; } A& operator= (A&&) { throw 0; } }; struct B : A { }; either of the following lines throws: A x (std::move (A ()); A x;...

Moving inserted container element if possible

I'm trying to achieve the following optimization in my container library: when inserting an lvalue-referenced element, copy it to internal storage; but when inserting rvalue-referenced element, move it if supported. The optimization is supposed to be useful e.g. if contained element type is something like std::vector, where moving if...

Is it bad form to provide only a move constructor?

I would like to return a noncopyable object of type Foo from a function. This is basically a helper object which the caller will use to perform a set of actions, with a destructor to perform some cleanup after the actions are complete. Before the advent of rvalue references, I would have returned a shared_ptr<Foo> or something similar. ...

Move constructor (rvalue reference) in implicit conversion

Hi all, I am upgrading a C++ project from MSVC 2008 to 2010, and because of the new CComBSTR move constructor [CComBSTR( CComBSTR&& )], I am getting a compiler error because of an ambiguous call. Essentially, we have a String class, very similar to std::wstring that have a cast operator to CComBSTR. This is similator to the following ...