shared_ptr< T []>的问题;包装动态数组 [英] Problems with shared_ptr<T[]> wrapping a dynamic array

查看:63
本文介绍了shared_ptr< T []>的问题;包装动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 std :: shared_ptr 替换班级中的一些原始指针,以便在创建该类的副本时不必担心。但是原始指针指向动态数组。当您给它提供自定义删除器(例如,things)时,可以将shared_ptr与动态数组一起使用。 default_delete< T []>

I wanted to replace some raw pointers in my class with a std::shared_ptr so that I don't have to worry when I create copies of that class. But the raw pointers point to a dynamic array. Using a shared_ptr with dynamic arrays is possible when you give it a custom deleter, e. g. default_delete<T[]>.

但是一旦我尝试分配,我就会得到一个很大的错误列表

But I get a big error list as soon as I try to assign a new value to that field, even on construction.

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

Here's a minimal code sample:

#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;
}






NB: 是的,我知道我可以/应该 vector 而不是动态数组。但是他们的表现是不一样的。我做了一些繁重的图像处理,并且数组容纳了像素。在低于VGA分辨率的情况下,处理时间从8秒增加到11秒。


NB: Yes, I know that I could/should vector instead of dynamic arrays. But their performance is not the same. I do some heavy image processing and the arrays hold the pixels. On less than VGA resolution the processing time increased from 8 to 11 s. That's quite a lot.

更新:当然,我可以在这里提供错误信息。我只是不知道是否应该将问题描述弄得一团糟。但是这里是:

Update: Of course I can provide the errors here. I just didn't know if I should clutter the problem description with it. But here it is:


C:\Program Files(x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\内存(754):错误C2664:'std :: _ Ptr_base< _Ty> :: _ Reset0':无法将参数1从'char '转换为'char()[]'

with

[

_Ty = char []

]

指向的类型无关。转换需要reinterpret_cast,C样式强制转换或函数样式强制转换

C:\程序文件(x86)\ Microsoft Visual Studio 11.0\VCCINCLUDE\memory(723):请参见参考函数模板实例化'void std :: shared_ptr< _Ty> :: __ Resetp0< _Ux>(_ Ux *,std :: _ Ref_count_base *)'正在编译



[

_Ty = char [],

_Ux = char

]

C:\Program Files(x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723):请参见对正在编译的函数模板实例化'void std :: shared_ptr< _Ty> :: _ Resetp0< _Ux>(_ Ux *,std :: _ Ref_count_base *)'的引用



[

_Ty = char [],

_Ux = char

]

C:\Program Files(x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494):请参见对函数模板实例化的引用'void std :: shared_pt r< _Ty> :: _ Resetp< _Ux,_Dx>(_ Ux *,_ Dx)'正在编译



[

_Ty = char [],

_Ux =字符,

_Dx = std :: default_delete

]

C:\程序文件(x86)\ Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494):请参见对正在编译的函数模板实例化'void std :: shared_ptr< _Ty> :: _ Resetp< _Ux,_Dx>(_ Ux *,_ Dx)'的引用< br>


[

_Ty = char [],

_Ux = char,

_Dx = std: :default_delete

]

problem.cpp(9):请参见对函数模板实例化'std :: shared_ptr< _Ty> :: shared_ptr>(_ Ux *,_ Dx)'的引用编译后的



[

_Ty = char [],

T = char,

_Ux =字符,

_Dx = std :: default_delete

]

problem.cpp(9):请参见正在编译的函数模板实例化'std :: shared_ptr< _Ty> :: shared_ptr>(_ Ux *,_ Dx)'的引用



[

_Ty = char [],

T = char,

_Ux = char,

_Dx = std :: default_delete

]

problem.cpp(21):请参见正在编译的函数模板实例化'std :: shared_ptr< _Ty> make_shared_array(size_t)'的引用< br>


[

_Ty = char []

]

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 []
]


推荐答案

您建议的解决方案是可能的,但您将失去数组的大小:

The solution you suggest is possible, but you will lose the size of the array:

#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;
}

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

What I have done here is to let the array decay into a pointer. As long as the deleter is an array deleter it should behave correctly.

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

To prevent this loss of size, and if you cannot use boost::shared_array as suggested, I would suggest to encapsulate this information in your own shared_array class.

这篇关于shared_ptr&lt; T []&gt;的问题;包装动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆