shared_ptr vs unique_ptr with arrays

c++, c++11, pointers, shared-ptr, unique-ptr

Solution

So some thoughts:

- `std::shared_ptr<char[]> ptr(new char[100]);` should be a syntax error all by itself.

- For whatever reason, `unique_ptr` was enabled to support the `std::unique_ptr<char[]>` syntax. Some of the rationale can be found here.

- The `std::shared_ptr<T>::operator[]` call also doesn't work, because `std::shared_ptr` does not implement it, only `std::unique_ptr` does.

Problem

this does not work: ``` std::shared_ptr <char[]> ptr(new char[100]); ptr[10] = '\0'; ``` and this works: ``` std::unique_ptr <char[]> ptr(new char[100]); ptr[10] = '\0'; ``` I get compliler error C2676, meaning that "does not define this operator or a conversion to a type acceptable to the predefined operator". Any idea why is it so?

Original source

Related problems