There is a specialization of unique_ptr
, like unique_ptr<T[]>
.
#include <iostream>
#include <memory>
struct test
{
~test() { std::cout << "test::dtor" << std::endl; }
};
int main()
{
std::unique_ptr<test[]> 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<T[]>
for deleter since it doesn't have one like shared_ptr<t[]>
.
std::shared_ptr<test> array(new test[3], std::default_delete<test[]>());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…