为什么 std::auto_ptr 上不允许使用运算符 []
为什么 std::auto_ptr 上不允许使用运算符 []?
#include <iostream>
using namespace std ;
template <typename T>
void foo( T capacity )
{
auto_ptr<T> temp = new T[capacity];
for( size_t i=0; i<capacity; ++i )
temp[i] = i; // Error
}
int main()
{
foo<int>(5);
return 0;
}
在 Microsoft Visual C++ 2010 上编译。
错误:错误 C2676:二进制 '[' : 'std::auto_ptr<_Ty>'未定义此运算符或转换为预定义运算符可接受的类型
Why operator [] is not allowed on std::auto_ptr?
#include <iostream>
using namespace std ;
template <typename T>
void foo( T capacity )
{
auto_ptr<T> temp = new T[capacity];
for( size_t i=0; i<capacity; ++i )
temp[i] = i; // Error
}
int main()
{
foo<int>(5);
return 0;
}
Compiled on Microsoft Visual C++ 2010.
Error: error C2676: binary '[' : 'std::auto_ptr<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
原因是
auto_ptr
将使用delete
而不是delete[]
来释放内容,因此auto_ptr
不是适合处理堆分配的数组(用new[]
构造),并且仅适合处理用new
构造的单个堆分配数组。支持
operator[]
会鼓励开发人员将其用于数组,并且会错误地给人一种该类型可以支持数组的印象,而实际上它不能。如果您想要一个类似智能指针的数组类,请使用 boost::scoped_array。
The reason is that
auto_ptr
will free the content usingdelete
instead ofdelete[]
, and soauto_ptr
is not suitable for handling heap-allocated arrays (constructed withnew[]
) and is only suitable for handling single, heap-allocated arrays that were constructed withnew
.Supporting
operator[]
would encourage developers to use it for arrays and would mistakenly give the impression that the type can support arrays when, in fact, it cannot.If you want a smartpointer-like array class, use boost::scoped_array.
因为
std::auto_ptr
不适合与数组一起使用。此外,在您的示例中
实际上分配了一个 single
int
并使用capacity
对其进行初始化。它不会像您预期的那样创建整数数组。Because
std::auto_ptr
is not intended to be used with arrays.Besides, In your sample
actually allocates a single
int
and initializes it withcapacity
. It does not create an array of integers as you seem to have intended.因为
auto_ptr
被设计为保存指向单个元素的指针;它将在销毁时使用delete
(特别是不是delete[]
)。你的例子并没有做(我认为)你认为的事情。或者至少名称“容量”具有误导性,因为您只分配一个元素(并将“容量”的值分配给它)。你的 for 循环没有任何意义。
Because
auto_ptr
is designed to hold a pointer to a single element; it will usedelete
(specifically notdelete[]
) on its destruction.Your example is not doing what (I think) you think it does. Or at least the name capacity is misleading, because you are only allocating a single element (and assigning the value of capacity` to it). Your for loop has no sensible meaning.
auto_ptr
和其他智能指针仅用于存储指向单个对象的指针。这是因为它们在析构函数中使用delete
,而不是delete[]
,如果要存储指向数组的指针,则需要使用delete[]
。如果您需要将对象数组包装在智能指针中,标准库不会提供任何帮助。不过,Boost 确实提供了
scoped_array
,其行为与std::auto_ptr
类似,用于保存由new[]
创建的对象数组。auto_ptr
and other smart pointers are only intended to store a pointer to a single object. This is because they usedelete
in the destructor, rather thandelete[]
, which would be needed if it were storing a pointer to an array.If you need to wrap an array of objects in a smart pointer, the standard library doesn't offer anything to help. However, Boost does offer
scoped_array
, which behaves similar tostd::auto_ptr
and is made to hold arrays of objects created bynew[]
.