C ++ 11和std :: vector构造函数中的值初始化对象 [英] Value-Initialized Objects in C++11 and std::vector constructor

查看:140
本文介绍了C ++ 11和std :: vector构造函数中的值初始化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,有少数令人信服的理由在 std :: vector 上使用C数组。这些少数令人信服的原因之一,至少对于C ++ 03,是不可能使用一个向量来分配一个未初始化的对象数组。 std :: vector 的填充构造函数为:



,const T& value = T())



意思是...

  int * array = new array [1000000]; 

可能比:

  std :: vector< int> v(1000000); 

...因为向量构造函数必须零初始化整数数组。因此,当使用POD的向量时,没有真正等价于 malloc ;你最好得到的是等价于 calloc



C ++ 11似乎改变了这一点, 价值初始化的概念。在C ++ 11中, std :: vector 有一个新的构造函数,它接受一个 size_type 论据。这种值初始化向量中的所有元素。 C ++ 11标准区分值初始化和零初始化。



我的理解是值初始化等同于调用默认构造函数 T 。如果 T 是像 int 的POD类型,则默认构造函数只是创建一个未初始化的整数。因此,在C ++ 11中,显式向量::向量(size_type计数)真的等效于 malloc if <



但是,我的理解是基于

问题:我的理解在这里是否正确? explicit vector :: vector(size_type count)提供未初始化数组(类似于 malloc )if T 是POD?

解决方案


:我的理解在这里吗? explicit vector :: vector(size_type count)提供未初始化数组
(类似于 malloc )if T 是POD?


否。 C ++ 03和C ++ 11之间有区别,但不是这样。区别在于,在C ++ 03中,向量(N)将默认构造一个 T 然后使 N 个副本填充向量。



而在C ++ 11中,<$ c $通过默认构造 T N (N) >次。对于POD类型,效果是相同的。事实上,我希望对于几乎所有类型的效果是一样的。然而,对于类似 unique_ptr (仅移动类型)的东西,差异是至关重要的。



所以:


$ b。C ++ 03语义不会工作,因为你不能创建一个移动类型的副本。 $ b

 矢量< unique_ptr< int>> v(10); 

创建一个包含10个null unique_ptrs(不是彼此的副本)的向量。



在极少数情况下,它会产生变化,你需要C ++ 03的行为,可以很容易地实现:

  vector< T> v(10,T()); 


In C++ there are few compelling reasons to use a C array over std::vector. One of those few compelling reasons, at least with C++03, was the fact that it is impossible to use a vector to allocate an uninitialized array of objects. The "fill" constructor for std::vector is:

vector(size_type count, const T& value = T())

Meaning that...

int* array = new array[1000000];

is likely to be much more efficient than:

std::vector<int> v(1000000);

...since the vector constructor will have to zero-initialize the array of integers. Thus, when working with a vector of PODs, there is no real equivalent to malloc; the best you can get is an equivalent to calloc.

C++11 seems to have changed this, with the concept of "value-initialization." In C++11, std::vector has a new constructor which takes a single size_type value, with no default argument. This "value-initializes" all elements in the vector. The C++11 standard distinguishes between "value-initialization" and "zero-initialization."

My understanding is that "value-initialization" is equivalent to calling the default constructor on T. If T is a POD type like int, then the default constructor simply creates an uninitialized integer. Thus, in C++11, explicit vector::vector(size_type count) is truly equivalent to malloc if T is a POD.

However, my understanding of this is based on the draft C++11 standard, rather than the final standard.

Question: Is my understanding correct here? Does explicit vector::vector(size_type count) provide an uninitialized array (similar to malloc) if T is a POD?

解决方案

Question: Is my understanding correct here? Does explicit vector::vector(size_type count) provide an uninitialized array (similar to malloc) if T is a POD?

No. There is a difference here between C++03 and C++11, but that isn't it. The difference is that in C++03, vector<T>(N) would default construct a T, and then make N copies of it to populate the vector.

Whereas in C++11, vector<T>(N) will populate the vector by default constructing T N times. For POD types the effect is identical. Indeed, I would expect that for almost all types the effect is identical. However for something like a unique_ptr (a move-only type), the difference is critical. The C++03 semantics would never work since you can not make a copy of a move-only type.

So:

vector<unique_ptr<int>> v(10);

creates a vector of 10 null unique_ptrs (which are not copies of each other).

In the rare case that it makes a difference and you need the C++03 behavior that can easily be accomplished with:

vector<T> v(10, T());

这篇关于C ++ 11和std :: vector构造函数中的值初始化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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