是标准定义的std :: array的大小 [英] Is the size of std::array defined by standard

查看:731
本文介绍了是标准定义的std :: array的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11 std :: array 被定义为具有连续的存储和性能,不逊于一个数组,但我不能决定是否标准的要求暗示std :: array具有与正常数组相同的大小和内存布局。这是你可以指望 sizeof(std :: array< int,N>)== sizeof(int)* N 还是具体实现?

In C++11 std::array is defined to have contiguous storage and performance that is no worse than an array, but I can't decide if the various requirements of the standard imply that std::array has the same size and memory layout as a normal array. That is can you count on sizeof(std::array<int,N>) == sizeof(int)*N or is that implementation specific?

特别是,这是保证以你期望的方式工作:

In particular, is this guaranteed to work the way you would expect it to:

std::vector< std::array<int, N> > x(M);
typedef (*ArrayPointer)[N];
ArrayPointer y = (ArrayPointer) &x[0][0];
// use y like normal multidimensional array

它在我尝试的两个编译器GNU& Intel)。此外,我可以找到的所有第三方文档( like this ),声明std :: array只是作为存储器高效的平面数组,其与连续的要求相结合将意味着它必须具有相同的存储器布局。但是我在标准中找不到这个要求。

It works in the two compilers I tried (GNU & Intel). Furthermore, all the 3rd party documentation I could find (like this), states that std::array is just as memory efficient as a plain array, which combined with the contiguous requirement would imply that it must have identical memory layout. However I can't find this requirement in the standard.

推荐答案

这是具体来说,§23.3.2.1/ 2说:

It's nearly required. Specifically, §23.3.2.1/2 says:


数组是一个聚合(8.5.1),可以使用语法

An array is an aggregate (8.5.1) that can be initialized with the syntax



array<T, N> a = { initializer-list };




其中 initializer-list 是一个逗号分隔的列表,最多可以有N个元素,其类型可以转换为T。

where initializer-list is a comma-separated list of up to N elements whose types are convertible to T.

不能使用任何类型的构造函数将初始化器列表中的数据转换为正确的格式。这真的只有一个可能性:它可以存储的唯一的东西是值本身。

Since it's an aggregate, it can't use any sort of constructor to convert the data in the initializer-list to the correct format. That really only leaves one possibility: about the only thing it can store are the values themselves.

我想它 code> std :: array 来存储指定数据之后的某些辅助数据,例如额外的内存设置为某个预定义值,因此如果你写过数组的末尾,可能会改变这些数据。然后,编译器/运行时会在关闭时检查这些值,如果您更改了值,则报告您的代码未定义的行为。

I suppose it would be possible for an std::array to store some sort of auxiliary data following the specified data, such as extra memory set to some predefined value, so if you write past the end of the array, you'd probably change that data. The compiler/run-time would then check those values at shut-down, and if you'd changed the values, report your code's undefined behavior.

对于 std :: array ,编译器 可以对内置数组进行不同的填充/排列。甚至可能需要这样做的一个明显的示例是支持超级对齐要求,例如用于与Intel的SSE指令一起使用的数据。内置数组不能支持超对齐,但我 std :: array

It's also possible that a compiler could do padding/alignment differently for an std::array than for a built-in array. One obvious example for which this could even be desirable would be to support super-alignment requirements, such as data for use with Intel's SSE instructions. A built-in array can't support super-alignment, but I think the specification of std::array might be barely loose enough to allow it.

底线:在不考虑可能存在多少可能性的问题,很清楚 std: :array 不一定必须遵循您要求的规则。

Bottom line: without getting into questions of how many possibilities might exist, it's pretty clear that std::array doesn't necessarily have to follow the rule you're asking about.

这篇关于是标准定义的std :: array的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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