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?

1 Answer 1

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for a good use of the relatively new term universal reference.
@MatthiasVallentin: That term was contrived on the spot by Scott Meyers. The standards committee prefers the term "forwarding reference" nowadays.
We're almost two years in now, glad to see that the community has converged on a stable term.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.