c++ - Marking std::unique_ptr class member as const -


a lot of examples using std::unique_ptr manage ownership of class dependencies following:

class parent { public:     parent(child&& child) :     _child(std::make_unique<child>(std::move(child))){} private:     std::unique_ptr<child> _child; }; 

my question whether marking _child member const have unexpected side effects? (aside being ensuring reset(), release() etc. cannot called on _child).

i ask since have not yet seen in example , don't whether intentional or brevity/generality.

because of nature of std::unique_ptr(sole ownership of object) it's required have no copy constructor whatsoever. move constructor(6) takes non-const rvalue-references means if you'd try make _child const , move you'd nice compilation error :)

even if custom unique_ptr take const rvalue-reference impossible implement.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -