Reputation: 2877
It seems an easy way to circumvent a unique_ptr
is to use a pointer to the unique_ptr
object. And that's not difficult. So using the unique_ptr is sort of a gentleman's agreement and not really super enforced?
#include <iostream>
#include <memory>
using namespace std;
class Box {
public:
int num;
};
void update_box(unique_ptr<Box>* pu);
int main(){
unique_ptr<Box> b{new Box};
unique_ptr<Box>* u = &b;
update_box(u);
cout << b->num << endl; // Outputs 99.
return 0;
}
void update_box(unique_ptr<Box>* pu) {
(*pu)->num = 99;
}
Upvotes: 8
Views: 17011
Reputation: 301
I know that this is answered by multiple people. But what I want to mention is that, there could be real usecase where you need to pass reference of unique_ptr.
Consider the below example :
StatusMsg getMemory(unique_ptr<char[]>& Up) {
unique_ptr<char[]> mem(new char[100]);
Up = std::move(mem);
return StatusMsg::Success;
}
int main() {
unique_ptr<char[]> Up;
StatusMsg s = getMemory(Up);
if (s!=StatusMsg::Success) {
// throw error
}
// Do something with allocated memory
}
Here the unique_ptr must be returned (or updated) using call by reference, since the actual return value should be the status msg.
Upvotes: 0
Reputation: 29952
You actually put your real question into a comment:
My understanding of
unique_ptr
is that it should be used to ensure you only have one pointer to an object at any time.
No, this is wrong. You can easily do a simple:
std::unique_ptr<int> a(new int);
int *b = a.get(); // b points to the same object as a
The design of unique_ptr
tries to ensure that you can only have one unique_ptr
to an object. But even, it can ensure this only if you don't use naked pointers. With naked pointers, it is easy to sidestep this design:
std::unique_ptr<int> a(new int);
std::unique_ptr<int> b(a.get());
Here, b
points to the same object as a
, so this program has undefined behavior (because the int
object will be deleted twice).
unique_ptr
's intent is to show ownership. As there can be only one unique_ptr
which can point to an object (ignoring the "hack" I've presented earlier), unique_ptr
owns the pointed object, and as unique_ptr
's destructor is called, it will delete the pointed object.
Upvotes: 14
Reputation: 234665
C++ is, in a sense, full of gentlemen's agreements. In other words, the language gives you the ability to shoot yourself in the foot.
There is nothing to prevent you taking the address of a std::unique_ptr
. If you really find that distasteful then you could inherit from std::unique_ptr
and overload the address-of operator with a function containing a static assertion.
But even if you did that, you could circumvent that with std::addressof
!
Upvotes: 19