TR1 Shared Arrays

前端 未结 2 510
陌清茗
陌清茗 2020-12-16 14:14

I\'ve had a hard time finding references in the TR1 documentation concerning shared arrays. The Boost documentation is fairly clear that there is a significant difference b

相关标签:
2条回答
  • 2020-12-16 14:48

    That is correct, there is no shared_array in TR1.

    You can, however, provide your own deleter object to perform "delete []" if you wish using this constructor:

    template<class Other, class D>
       shared_ptr(Other* ptr, D dtor);
    

    For example:

    template<typename T>
    struct my_array_deleter
    {
       void operator()(T* p)
       {
          delete [] p;
       }
    };
    
    shared_ptr<int> sp(new int[100], my_array_deleter<int>());
    
    0 讨论(0)
  • 2020-12-16 14:57

    I suspect that most people who use TR1 do not use arrays, but use vector<> instead.

    I haven't read TR1, so I'll answer on the basis of Boost, which is probably good enough. boost::shared_ptr<> deals with individual objects, and not arrays. That's what boost::shared_array<> is for.

    If you're using arrays, and have reasons to convert to shared_array<> but not to vector<>, use shared_array<>.

    0 讨论(0)
提交回复
热议问题