为什么std :: vector的元素不需要默认的构造函数? [英] Why don't std::vector's elements need a default constructor?

查看:142
本文介绍了为什么std :: vector的元素不需要默认的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又如何编写自己的数组类,以使其不需要元素的默认构造函数?现在,当我执行new []来分配空间时,我需要一个默认的构造函数。

And how can I write my own array class to not need a default constructor for its elements? Right now, when I do the new [] to allocate space, I need a default constructor.

std :: vector不需要。

std::vector does not.

他们是如何做到这一魔术的?

How do they do this magic?

推荐答案

std :: vector 不需要默认构造函数,因为它从不使用它。每次需要构造一个元素时,都使用 copy构造器来完成它,因为每次它都有要复制的东西:现有的vector元素或您自己提供的用于通过方法的参数进行复制的元素(通过默认参数显式或隐式地)

std::vector doesn't need the default constructor because it never uses it. Every time it needs to construct an element, it does it by using the copy constructor, because every time it has something to copy: either existing vector element or an element you yourself supplied for copying through a method's parameter (explicitly or implicitly, by relying on a default argument)

您可以以完全相同的方式编写此类:每次需要在数组中构造一个新元素,要求用户提供要复制的元素。在这种情况下,构造原始元素将成为用户的责任。

You can write a class like that in exactly the same way: every time you need to construct a new element in your array, require the user to supply an element for copying. In this case constructing that original element becomes user's responsibility.

每次看起来好像 std :: vector 要求来自您的默认构造函数,它只是意味着您依赖某个 vector s方法的默认参数的地方,即它是 you 谁试图默认构造一个元素,而不是向量。向量本身再也不会尝试默认构造元素。

Every time it appears as if std::vector "requires" a default constructor from you, it simply means that somewhere you relied on a default argument of some of the vectors methods, i.e. it was you who tried to default-construct an element, not the vector. The vector itself, again, will never try to default-construct elements.

为了避免在分配内存时使用默认构造函数,标准库会分配未初始化的 raw 内存块,然后立即复制构造该原始内存块中的新元素(这是 new [] 无法做到的)。此功能封装在 std :: allocator 类中。您也可以在代码中使用 std :: allocator ,这意味着魔术也立即可用。

In order to avoid the default constructor requirement during memory allocation, standard library allocates raw uninitialized memory block and then immediately copy-constructs new elements in that raw memory block (which is something new[] cannot do). This functionality is incapsulated in std::allocator class. You can use std::allocator in your code as well, meaning that the "magic" is immediately available to you too.

注意::以上内容适用于C ++ 11之前的C ++语言规范版本。 C ++ 11改变了很多事情。这些更改确实会导致 std :: vector 可以在内部使用默认构造函数的情况。

Note: The above applies to pre-C++11 version of C++ language specification. C++11 changed a lot of things. And these changes do create situations in which std::vector can use default constructors internally.

另外,也许值得注意的是,即使原始的C ++ 98规范也允许实现使用函数重载代替默认参数以实现标准库接口。这意味着在形式上可以有一个有效的 std :: vector 的C ++ 98实现,该实现使用内部默认构造函数

Also it might be worth noting that even the original C++98 specification allowed implementations to use function overloading instead of default arguments in order to implement the standard library interface. This means that formally it is possible to have a valid C++98 implementation of std::vector that uses default constructors internally.

这篇关于为什么std :: vector的元素不需要默认的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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