Вопрос

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?

Это было полезно?

Решение

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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top