TR1 共享阵列
我很难在 TR1 文档中找到有关共享数组的参考资料。 Boost 文档相当清楚地表明,C++“new”和“new[]”表达式之间存在显着差异。 Shared_ptr 模板旨在正确保存指向使用“new”创建的动态分配对象的指针。 Shared_array 模板旨在使用“new[]”正确保存指向动态分配数组的指针。
我正在更新一些代码以使用 TR1 shared_ptr 模板和相关函数,但我发现没有提到shared_array。 TR1 shared_ptr 实现是否区分“new”和“new[]”,并正确销毁这些指针? 据我从 TR1 规格来看,似乎并非如此。 如果是这种情况,我是否仍然应该使用 boost shared_array 模板进行“new[]”样式分配?
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 between the C++ "new" and "new[]" expressions. The shared_ptr template is meant to correctly hold a pointer to a dynamically allocated objected created using "new". The shared_array template is meant to correctly hold a pointer to a dynamically allocated array using "new[]".
I'm in the process of updating some code to use the TR1 shared_ptr template and associated functions, but I've found no mention of shared_array. Does the TR1 shared_ptr implementation differentiate between "new" and "new[]", and destroy those pointers correctly? As far as I can tell from looking at the TR1 spec, it appears it does not. If this is the case, should I still be using the boost shared_array template for "new[]" style allocations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没错,TR1中没有shared_array。
但是,如果您希望使用此构造函数,您可以提供自己的删除器对象来执行“delete []”:
例如:
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:
For example:
我怀疑大多数使用TR1的人都没有使用数组,而是使用vector<> 反而。
我没有读过TR1,所以我会在Boost的基础上回答,这可能已经足够好了。 boost::shared_ptr>> 处理单个对象,而不是数组。 这就是 boost::shared_array<> 的作用。 是为了.
如果您使用数组,并且有理由转换为共享数组<>; 但不是向量<>,而是使用shared_array<>。
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<>.