7

I want to implement move constructors (no copy constructor) for a certain type that needs to be a value type in a boost::unordered_map. Let's call this type Composite.

Composite has the following signature:

struct Base
{
  Base(..stuff, no default ctor) : initialization list {}
  Base(Base&& other) : initialization list {} 
}

struct Composite
{
  Base member;
  Composite(..stuff, no default ctor) : member(...) {}
  Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base
}

I want to write this so boost::unordered_map< Key , Composite > does not require the copy constructor, and just uses the move constructor. If possible, I don't want to use the copy constructor of Base in the initialization list of move constructor of Composite.

Is this possible?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
lurscher
  • 25,930
  • 29
  • 122
  • 185

1 Answers1

16

Say member(std::move(other.member)).

As a golden rule, whenever you take something by rvalue reference, you need to use it inside std::move, and whenever you take something by universal reference (i.e. deduced templated type with &&), you need to use it inside std::forward.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    +1 for a good use of the relatively new term *universal reference*. – mavam Dec 11 '12 at 02:53
  • 1
    @MatthiasVallentin: That term was contrived on the spot by Scott Meyers. The standards committee prefers the term "forwarding reference" nowadays. – Kerrek SB Oct 29 '14 at 12:35
  • We're almost two years in now, glad to see that the community has converged on a stable term. – mavam Oct 31 '14 at 21:36