gpt4 book ai didi

c++ - shared_ptr 包装动态数组的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:28 29 4
gpt4 key购买 nike

我想用 std::shared_ptr 替换我类(class)中的一些原始指针这样我在创建该类的拷贝时就不必担心了。但是原始指针指向一个动态数组。当你给它一个自定义删除器时,使用带有动态数组的 shared_ptr 是可能的,例如。 G。 default_delete<T[]> .

但是当我尝试为该字段分配一个新值时,我会得到一个很大的错误列表,即使是在构建时也是如此。

这是一个最小的代码示例:

#include <memory>
#include <cstddef>

using namespace std;

template<typename T> shared_ptr<T[]> make_shared_array(size_t size)
{
return shared_ptr<T[]>(new T[size], default_delete<T[]>());
}

struct Foo
{
shared_ptr<char[]> field;
};

int main()
{
Foo a;
// This line produces the error.
a.field = make_shared_array<char>(256);

return 0;
}

注意:是的,我知道我可以/应该vector而不是动态数组。但是他们的表现是不一样的。我做了一些繁重的图像处理,阵列保存像素。在低于 VGA 分辨率的情况下,处理时间从 8 秒增加到 11 秒。这是相当多的。


更新:当然我可以在这里提供错误。我只是不知道我是否应该用它来混淆问题描述。但这里是:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(754) : error C2664: 'std::_Ptr_base<_Ty>::_Reset0' : cannot convert parameter 1 from 'char ' to 'char ()[]'
with
[
_Ty=char []
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723) : see reference to function template instantiation 'void std::shared_ptr<_Ty>::_Resetp0<_Ux>(_Ux *,std::_Ref_count_base *)' being compiled
with
[
_Ty=char [],
_Ux=char
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723) : see reference to function template instantiation 'void std::shared_ptr<_Ty>::_Resetp0<_Ux>(_Ux *,std::_Ref_count_base *)' being compiled
with
[
_Ty=char [],
_Ux=char
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494) : see reference to function template instantiation 'void std::shared_ptr<_Ty>::_Resetp<_Ux,_Dx>(_Ux *,_Dx)' being compiled
with
[
_Ty=char [],
_Ux=char,
_Dx=std::default_delete
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494) : see reference to function template instantiation 'void std::shared_ptr<_Ty>::_Resetp<_Ux,_Dx>(_Ux *,_Dx)' being compiled
with
[
_Ty=char [],
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(9) : see reference to function template instantiation 'std::shared_ptr<_Ty>::shared_ptr>(_Ux *,_Dx)' being compiled
with
[
_Ty=char [],
T=char,
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(9) : see reference to function template instantiation 'std::shared_ptr<_Ty>::shared_ptr>(_Ux *,_Dx)' being compiled
with
[
_Ty=char [],
T=char,
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(21) : see reference to function template instantiation 'std::shared_ptr<_Ty> make_shared_array(size_t)' being compiled
with
[
_Ty=char []
]

最佳答案

您建议的解决方案是可行的,但您会损失数组的大小:

#include <memory>
#include <cstddef>

using namespace std;

template<typename T> shared_ptr<T> make_shared_array(size_t size)
{
return shared_ptr<T>(new T[size], default_delete<T[]>());
}

struct Foo
{
shared_ptr<char> field;
};

int main()
{
Foo a;
a.field = make_shared_array<char>(256);

return 0;
}

我在这里所做的是让数组衰减为指针。只要删除器是数组删除器,它就应该正确运行。

为了防止这种大小损失,如果您不能按照建议使用 boost::shared_array,我建议将此信息封装在您自己的 shared_array 类中。

关于c++ - shared_ptr<T[]> 包装动态数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957359/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com