I understand that it std::unique_ptris as it is and probably will not be changed to cancel backward compatibility, but I was wondering if anyone has a good reason why the spec authors did not overload the method getwith the const option, which looks like
const T* get() const;
follow the intention to unique_ptrbe const.
My best guess is that it is trying to flip pointers and act like T* constinstead of a regular class. As a follow-up question, if I wanted to keep a pointer in a const-like module in a const instance of my class, should I use something other than std::unique_ptrto store data?
Update
In my case, I want to protect myself from misusing the pointer in the class itself. I wrote a const move constructor MyClass(const MyClass&& other)and copied the data from the new instance to another through std::copy. It took a long time to track down the error, because I assumed that the copy should be correct due to real-time protection. I am trying to figure out what I could do to protect myself from this outside of providing a const getter and using it in the class when executing the copy.
source
share