Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret a "const unique_ptr"

const unique_ptr<int> upi{new int{4}};
cout << ++*upi << '\n';

Seems upi is a const smart pointer. The int it points to can still be modified. What if I want to declare the int as const?

const unqiue_ptr<int> const upi{new int{4}};    //Error: duplicate const
like image 927
Milo Lu Avatar asked Nov 27 '22 20:11

Milo Lu


1 Answers

As far as I understand, you want to make the integer const and not the pointer itself, right? Then you would have to write:

unqiue_ptr<const int> upi{new int{4}}; 
like image 130
tonisuter Avatar answered Dec 05 '22 09:12

tonisuter