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
Post a Comment