-6

Can you tell me if this works in VS2015 ?

shared_ptr< char> buffer( make_shared< array< char,10>>() , [] (char *p){delete[] p; } );

or

shared_ptr< char> buffer( make_shared< array< int,10>>() ,default_delete< int[]>());
kocica
  • 6,172
  • 2
  • 11
  • 34

1 Answers1

0

Visual Studio 2015 doesn't support the C++17 standard. Prior to C++17 standard you can't have the std::shared_ptr<T[]> pointer. But even in C++17 the std::make_shared function doesn't support array types so you would have to use the boost::make_shared instead. Another alternative is to use the unique pointer in combination with the std::make_unique which does support the array types. This again might not be a good idea as pointed out by Scot Meyers in his "Effective Modern C++" book:

The existence of std::unique_ptr for arrays should be of only intellectual interest to you, because std::array, std::vector, and std::string are virtually always better data structure choices than raw arrays.

Ron
  • 13,116
  • 3
  • 25
  • 45
  • You might want to look at [this answer](https://stackoverflow.com/questions/13061979/shared-ptr-to-an-array-should-it-be-used/13062069#13062069) as was suggested to me by @Baum mit Augen. – Ron Aug 17 '17 at 23:25
  • That answer uses explicit new, and my problem here is to get rid of it. std::shared_ptr p(new int[10], std::default_delete()); // works fine in VS2015. Is it possible to get rid of the explicit new using make_shared ? Thanks – ahlougha shad Aug 18 '17 at 00:09