为什么 std::auto_ptr 上不允许使用运算符 []

发布于 2024-10-24 08:57:22 字数 480 浏览 11 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美煞众生 2024-10-31 08:57:22

原因是 auto_ptr 将使用 delete 而不是 delete[] 来释放内容,因此 auto_ptr 不是适合处理堆分配的数组(用 new[] 构造),并且仅适合处理用 new 构造的单个堆分配数组。

支持 operator[] 会鼓励开发人员将其用于数组,并且会错误地给人一种该类型可以支持数组的印象,而实际上它不能。

如果您想要一个类似智能指针的数组类,请使用 boost::scoped_array

The reason is that auto_ptr will free the content using delete instead of delete[], and so auto_ptr is not suitable for handling heap-allocated arrays (constructed with new[]) and is only suitable for handling single, heap-allocated arrays that were constructed with new.

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.

-残月青衣踏尘吟 2024-10-31 08:57:22

因为 std::auto_ptr 不适合与数组一起使用。

此外,在您的示例中

std::auto_ptr<T> temp = new T(capacity); // T=int, capacity=5

实际上分配了一个 single int 并使用 capacity 对其进行初始化。它不会像您预期的那样创建整数数组。

Because std::auto_ptr is not intended to be used with arrays.

Besides, In your sample

std::auto_ptr<T> temp = new T(capacity); // T=int, capacity=5

actually allocates a single int and initializes it with capacity. It does not create an array of integers as you seem to have intended.

硬不硬你别怂 2024-10-31 08:57:22

因为 auto_ptr 被设计为保存指向单个元素的指针;它将在销毁时使用delete(特别是不是delete[])。

你的例子并没有做(我认为)你认为的事情。或者至少名称“容量”具有误导性,因为您只分配一个元素(并将“容量”的值分配给它)。你的 for 循环没有任何意义。

Because auto_ptr is designed to hold a pointer to a single element; it will use delete (specifically not delete[]) 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.

老街孤人 2024-10-31 08:57:22

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 use delete in the destructor, rather than delete[], 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 to std::auto_ptr and is made to hold arrays of objects created by new[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文