4

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?

KJS
  • 342
  • 1
  • 7

1 Answers1

3

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.
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • thanks for the reply. std::shared_ptr ptr(new char[100]); works with vs2013 compiler. – KJS Nov 24 '13 at 18:10