将 make_shared 与 char[] 或 int[] 一起使用

Using make_shared with char[] or int[]

你能告诉我这在 VS2015 中是否有效?

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

shared_ptr< char> buffer( make_shared< array< int,10>>() ,default_delete< int[]>());

Visual Studio 2015 不支持 C++17 标准。在 C++17 标准之前你不能有 std::shared_ptr<T[]> 指针。但即使在 C++17 中, 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 也支持数组类型。正如 Scot Meyers 在他的“有效 《现代 C++》一书:

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.