是否性病::阵列默认初始化或值初始化? [英] Does std::array default-initialize or value-initialize?
问题描述
根据 CP preference ,创建一个
的构造函数执行默认初始化。然而,当我做在Visual Studio 12.0一些测试,在某些情况下的std ::阵列
时的std ::阵列的std ::阵列
似乎在进行值初始化。
的std ::阵列< INT,3> ARR1; //给我一些垃圾值,符合市场预期
汽车ARR2 =的std ::阵列< INT,3 GT;(); //给我三0,值初始化?
此外,当的std ::阵列
是一个类的成员,它也有不确定的值,同时它也有全零。
类容器{
上市:
容器() ... INT和放大器;运算符[](为size_t我){返回改编[I] }
为size_t大小(){返回ARR_SIZE; }私人的:
静态常量为size_t ARR_SIZE = 3;
的std ::阵列< INT,ARR_SIZE> ARR;
};
在构造方法中没有明确定义或改编
不是在成员初始化列表,改编
包含不定值
集装箱(){} // ARR有不确定值,同样为没有构造的情况下
在改编
是在成员初始化列表,改编
包含所有零。
集装箱():
ARR()//改编包含0,0,0
{}
另外,当我写了下面的code,我得到一个错误。
集装箱():
改编{0,1,2}
{}
G:\\ cppconsole \\ cppconsole \\ main.cpp中(89):错误C2797:'集装箱::改编:内部成员初始化列表或者非静态数据成员初始化列表初始化不落实
块引用>应该根据C ++的新标准是有效的code?如果是这样,它只是我的Visual Studio版本不支持呢?
要具有相同的效果,我写了下面的code。有没有在code任何潜在的问题?因为我不知道,如果code是正确的。
集装箱():
ARR(decltype(ARR){0,1,2})
{}P.S。我使用Microsoft Visual Studio 2013社区
解决方案
的std ::阵列
是聚合,它不具有构造函数(这就是故意的)。所以,当默认初始化,它默认初始化它的元素。当值初始化,它的价值初始化它们。要分析你的code:
的std ::阵列< INT,3> ARR1; //给我一些垃圾值,符合市场预期
汽车ARR2 =的std ::阵列< INT,3 GT;(); //给我三0,值初始化?
ARR1
是默认初始化。ARR2
是复制初始化从临时将其与初始化()
,即值初始化。集装箱(){}
这离开成员
改编
默认初始化。集装箱():
ARR()//改编包含0,0,0
{}这初始化
改编
使用()
,这是价值的初始化。集装箱():
改编{0,1,2}
{}这是合法的C ++ 11,但2013 VS显然不支持它(它的C ++ 11的支持是不完整的)。
According to cppreference,
std::array
's constructor performs default-initialization when anstd::array
is created. However, when I'm doing some test in Visual Studio 12.0, in some circumstancesstd::array
seems to be performing value-initialization.std::array<int, 3> arr1; // gives me some garbage values, as expected auto arr2 = std::array<int, 3>(); // gives me three 0, value-initialize?
Also, when
std::array
is a member of a class, sometimes it has indeterminate values while sometimes it has all zero.class Container { public: Container() ... int& operator[](size_t i) { return arr[i]; } size_t size() { return ARR_SIZE; } private: static const size_t ARR_SIZE = 3; std::array<int, ARR_SIZE> arr; };
When the constructor is not explicitly defined or
arr
is not in the member initializer list,arr
contains indeterminate values.Container() {} // arr has indeterminate values, same for no constructor case
When
arr
is in the member initializer list,arr
contains all zero.Container(): arr() // arr contains 0, 0, 0 {}
Also, when I write the following code, I get an error.
Container() : arr{ 0, 1, 2 } {}
g:\cppconsole\cppconsole\main.cpp(89): error C2797: 'Container::arr': list initialization inside member initializer list or non-static data member initializer is not implemented
Is the code supposed to be valid according to the new standard of C++? If so, is it just my Visual Studio version does not support it?
To have the same effect, I write the following code. Is there any potential problem in the code? Because I'm not sure if the code is correct.
Container() : arr( decltype(arr){ 0, 1, 2 } ) {}
P.S. I'm using Microsoft Visual Studio Community 2013.
解决方案
std::array
is an aggregate, it does not have constructors (and that's intentional). So when default-initialised, it default-initialises its elements. When value-initialised, it value-initialises them.To analyse your code:
std::array<int, 3> arr1; // gives me some garbage values, as expected auto arr2 = std::array<int, 3>(); // gives me three 0, value-initialize?
arr1
is default-initialised.arr2
is copy-initialised from a temporary which was initialised with()
, i.e. value-initialised.Container() {}
This leaves the member
arr
default-initialised.Container(): arr() // arr contains 0, 0, 0 {}
This initialises
arr
using()
, which is value-initialisation.Container() : arr{ 0, 1, 2 } {}
This is legal C++11, but VS 2013 apparently doesn't support it (its C++11 support is incomplete).
这篇关于是否性病::阵列默认初始化或值初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!