Does C++11 have wrappers for dynamically-allocated arrays like Boost's scoped_array?

后端 未结 2 498
耶瑟儿~
耶瑟儿~ 2020-12-13 01:05

I often need to deal with dynamically-allocated arrays in C++, and hence rely on Boost for scoped_array, shared_array, and the like. After reading through Stroustrup\'s C++1

2条回答
  •  被撕碎了的回忆
    2020-12-13 01:16

    There is a specialization of unique_ptr, like unique_ptr.

    #include 
    #include 
    
    struct test
    {
      ~test() { std::cout << "test::dtor" << std::endl; }
    };
    
    int main()
    {
      std::unique_ptr array(new test[3]);
    }
    

    When you run it, you will get this messages.

    test::dtor
    test::dtor
    test::dtor
    

    If you want to use shared_ptr, you should use std::default_delete for deleter since it doesn't have one like shared_ptr.

    std::shared_ptr array(new test[3], std::default_delete());
    

提交回复
热议问题