The first time I tried to write a range based on a loop to iterate over unique_ptrs, I wrote:
std::vector<std::unique_ptr<Foo>> vec;
Then I realized that this is an attempt to create a copy of each element, which does not make sense with unique_ptr. So, I wrote this as a link:
for (auto& v : vec) {}
Adding a constant in front of it prevents me from changing the pointer.
for (const auto& v : vec) { v = nullptr;
How can I write it so that the data it points to cannot be changed? For example, the following code should not be compiled.
for (??? v : vec) { v->func(); } class Foo { public: void func(); private: bool mBar; } Foo::func() { mbar = true;
c ++ unique-ptr
user870130
source share