std :: array< char,N>的大小是多少? [英] What is the sizeof std::array<char, N>?

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

问题描述

C ++标准对sizeof(std::array<char, N>)应该是什么(对于某些常量N)说了什么?

What does the C++ standard say about what sizeof(std::array<char, N>) should be (for some constant N)?

std::array放置太大的常量可能会由于堆栈分配"变量的资源不足而导致程序中止.我认为后续评论意味着std::array可能会以某种方式切换到动态分配模式.

In a comment to a different question, it was mentioned that std::array is not always "stack allocated". The comment was in response to a different comment that speculated that putting a too large of a constant for std::array that is declared as a local variable could cause the program to abort due to insufficient resources for the "stack allocated" variable. I assume the followup comment meant that it would be possible for std::array to somehow switch to a dynamic allocation mode.

我可以想象对于数组大小阈值可以应用某种SFINAE,该阈值会触发std::array的特殊化,而实际上可以动态分配并管理数组.在这种情况下,sizeof(std::array<...>)可能只是指针的大小.允许发生这种情况吗?

I could imagine that there could be some kind of SFINAE could be applied for an array size threshold that triggers a specialization of std::array that actually dynamically allocates an array and manages it. In that case, the sizeof(std::array<...>) might just be the size of a pointer. Is that allowed to happen?

推荐答案

如果N == 0,显然是sizeof(std::array<char, N>) != N.它也不一定适用于N > 0. §23.3.2.1[array.overview]/p1-2:

Obviously sizeof(std::array<char, N>) != N if N == 0. It also doesn't necessarily hold for N > 0. §23.3.2.1 [array.overview]/p1-2:

头文件<array>定义用于存储固定大小的类模板 对象序列.数组支持随机访问迭代器.一个 array<T, N>的实例存储类型为TN元素,因此 size() == N是不变量.数组的元素被存储 连续地表示,如果aarray<T, N>,则它遵循 所有0 <= n < N的身份&a[n] == &a[0] + n.

The header <array> defines a class template for storing fixed-size sequences of objects. An array supports random access iterators. An instance of array<T, N> stores N elements of type T, so that size() == N is an invariant. The elements of an array are stored contiguously, meaning that if a is an array<T, N> then it obeys the identity &a[n] == &a[0] + n for all 0 <= n < N.

一个数组是一个 可以使用语法初始化的聚合(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.

第8.5.1节[dcl.init.aggr]/p1:

§8.5.1 [dcl.init.aggr]/p1:

聚集是没有用户提供的数组或类(第9条) 构造函数(12.1),没有私有或受保护的非静态数据成员 (第11条),没有基类(第10条)和虚拟函数 (10.3).

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

由于array是聚合类型,因此它不能具有执行动态分配的自定义构造函数,并且必须直接存储元素,因为必须能够使用聚合初始化从初始化列表中对其进行初始化.但是,只要array<T, N> a = { initializer-list };具有定义的语义(当 initializer-list 最多包含N个成员时),标准中的任何内容都不会阻止实现在其C样式数组成员之后添加额外的内容. .看起来像

Since array is an aggregate type, it can't have a custom constructor that performs dynamic allocation, and it must store the elements directly since it must be able to be initialized from a initializer list using aggregate initialization. However, nothing in the standard prevents the implementation from adding extra stuff after its C-style array member, as long as array<T, N> a = { initializer-list }; has the defined semantics when the initializer-list contains at most N members. An implementation that looks like

template<typename T, size_t N>
struct array {
    //typedefs and member functions omitted

    T _Elems[N];
    double _Because_I_can;
};
// specialization for N == 0 case omitted

是完全合法的.因此,不能保证sizeof(std::array<char, N>) == N.

is perfectly legal. Therefore, there's no guarantee that sizeof(std::array<char, N>) == N.

这篇关于std :: array&lt; char,N&gt;的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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