为std大小::阵列< T,N>保证是等于T [N]的大小? [英] Is the size of std::array<T, N> guaranteed to be equal to the size of T[N]?

查看:130
本文介绍了为std大小::阵列< T,N>保证是等于T [N]的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来这code ++工程,(所以它编译罚款),我问这里是什么?它是保证的sizeof(的std ::数组)是一样的sizeof(equivalent_Carray)

 结构MyClass的{
  的std ::阵列<浮动,4为H. ARR;
  浮卡尔[4];  的std ::阵列<浮动,4为H. cfunction(){
    的std ::阵列<浮动,的sizeof(卡尔)/的sizeof(浮动)GT;出;
    返回的;
  }  的std ::阵列<浮动,4为H.功能(){
    //难道这保证是一样的sizeof(卡尔)/的sizeof(浮动)?
    的std ::阵列<浮动,的sizeof(ARR)/的sizeof(浮动)GT;出;
    性病::法院LT&;<的sizeof(ARR);
    返回的;
  }
};诠释的main()
{
    MyClass的OBJ;
    obj.function();
}


解决方案

我们用 STD中得到的唯一保证::阵列< T,N> 是:


  • 尺寸() N ;

  • &放大器;一个[N] ==&放大器;一个[0] + N 所有 0℃; = N< ñ;

  • 数组是一个集合体(8.5.1),可以使用语法被初始化

     阵列< T,N> A = {初始化列表};


有什么从加入会员后停止执行者,虽然我知道没有理由一会。

It seems this code works (so it compiles fine), what I'm asking here is: is it guaranteed that sizeof(std::array) is the same as sizeof(equivalent_Carray)?

struct MyClass {
  std::array<float, 4> arr;  
  float carr[4];

  std::array<float, 4> cfunction() {
    std::array<float, sizeof(carr) / sizeof(float)> out;
    return out;
  }

  std::array<float, 4> function() {
    // Is this guaranteed to be the same as sizeof(carr) / sizeof(float)??
    std::array<float, sizeof(arr) / sizeof(float)> out;
    std::cout << sizeof(arr);
    return out;
  }
};

int main()
{
    MyClass obj;
    obj.function();
}

解决方案

No.

The only guarantees we get with std::array<T, N> are that:

  • size() is N;
  • &a[n] == &a[0] + n for all 0 <= n < N;
  • An array is an aggregate (8.5.1) that can be initialized with the syntax

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

There is nothing stopping the implementor from adding a trailing member, though I know of no reason why one would.

这篇关于为std大小::阵列&LT; T,N&GT;保证是等于T [N]的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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