相关疑难解决方法(0)

对于带有数组的unique_ptr有什么用处吗?

std::unique_ptr 支持数组,例如:

std::unique_ptr<int[]> p(new int[10]);
Run Code Online (Sandbox Code Playgroud)

但它需要吗?可能使用std::vector或更方便std::array.

你觉得这个结构有用吗?

c++ smart-pointers c++11

219
推荐指数
12
解决办法
13万
查看次数

为什么没有std :: shared_ptr <T []>专门化?

该标准提供了一个模板特化std::unique_ptr,正确调用delete[]其析构函数:

void func()
{
   std::unique_ptr< int[] > arr(new int[10]);

   .......
}
Run Code Online (Sandbox Code Playgroud)

由于std::shared_ptr这种专业化不可用,因此有必要提供一个正确调用的删除器delete[]:

void func()
{
    // Usage
    shared_ptr array (new double [256], [](double* arr) { delete [] arr; } ); 

    ..............
}
Run Code Online (Sandbox Code Playgroud)

这只是一个疏忽吗?(以同样的方式存在std::copy_if)或是否有原因?

c++ shared-ptr c++11

64
推荐指数
1
解决办法
9851
查看次数

标签 统计

c++ ×2

c++11 ×2

shared-ptr ×1

smart-pointers ×1