Quick Q: Marking std::unique_ptr class member as const

Quick A: It prevents you of being able to move your class.

Recently on SO:

Marking std::unique_ptr class member as const

Because of the nature of a std::unique_ptr (sole ownership of an object) it's required to have no copy constructor whatsoever. The move constructor(6) only takes non-const rvalue-references which means that if you'd try to make your _child const and move it you'd get a nice compilation error smile

Even if a custom unique_ptr would take a const rvalue-reference it would be impossible to implement.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.