在c样式数组上的std :: array [英] std::array over c style array

查看:122
本文介绍了在c样式数组上的std :: array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,在C ++中,应该始终在c样式数组上使用 std :: array

I was told that in C++, one should always use std::array over c-style array.

查看问题后,我发现人们说std :: array更好。

After viewing around questions I come across people saying that std::array is better.

,我使用了很多 std :: array ,有时当我使用其他库时,我不得不使用采用c样式数组的方法。

In that sense, I used a lot of std::array and sometimes when I use other libraries, I have to use methods that takes in c-style arrays.

例如,我正在使用此功能

For example, I am working with this function

void Draw(float* arg);

是否可以传递 std :: array< float,4> 作为参数?

Is it possible to pass in std::array<float,4> as a parameter?

如果我将& vec4 [0]作为参数传递,会不会有未定义的行为?

Will there be any undefined behavior if I were to pass in &vec4[0] as the parameter?

从这个意义上说,多维数组会一样吗?

In that sense, will it be the same for multi-dimensional arrays?

将此数组声明为

std::array<std::array<float,4>,4>;

如果我要传递& mtx4 [0] [0],还会有任何未定义的行为

Will there also be any undefined behavior if I were to pass in &mtx4[0][0] as the parameter?

编辑:感谢您指出我在多维数组代码中的错误。我对其进行了编辑。

Thanks for pointing out my error in the code for the multidimensional array. I edited it.

推荐答案


是否可以将std :: array作为参数传递?

Is it possible to pass in std::array as a parameter?

是。正确的语法是:

Draw(my_array.data());




如果我要传递& vec4 [0]作为参数?

Will there be any undefined behavior if I were to pass in &vec4[0] as the parameter?

否。从语法上讲,它并不能使您的意图变得清晰,但从语义上讲,它是相同的。

No. Syntactically it is less good at making your intentions clear, but semantically it is the same.


从这个意义上说,多维数组?

In that sense, will it be the same for multi-dimensional arrays?

是的,如果您是指经典的C多维数组,它实际上只是与索引信息相关联的平面数组。不,如果您的意思是C数组。

Yes, if you mean classic C multi-dimensional arrays, which are really just flat arrays associated with indexing information. No, if you mean C array-of-array.

float array2d[5][5]; // ok
float *array2d[5]; // not ok
std::array<std::array<float,5>,5> array2d; // not ok

第一个具有连续存储。另外两个是指针数组,必须以不同的方式处理。这里没有足够的建议方法。

The first has contiguous storage. The other two are arrays of pointer-to-array and have to be handled differently. There is not enough here to recommend how.

我找到了参考文献此处表示多维数组的另一种/新语法。

I found a reference here to another/new syntax for multi-dimensional arrays. It should provide contiguous storage.

std::array<float,5,5>; // should be ok

我真的不知道这种语法的状态是什么。也许其他人可以提供帮助。

I really have no idea what the status of this syntax is. Perhaps others can assist.

对此声明进行进一步调查后:

After some further investigation of this declaration:

std::array<std::array<float,5>,5> array2d;

这里的array2d的存储都是内联的,没有指针。在所有情况下,我都能够调查存储似乎是连续的,因此内存布局与

The storage for array2d here is all in-line, with no pointers involved. In all the cases I was able to investigate the storage appears to be contiguous, so that the memory layout is the same as for

float array2d[5][5];

这不是标准的要求。符合条件的实现可以插入其他信息或填充,以使 sizeof(arrayT)> 大于 sizeof(T [])表示某些T。如果这样做,那么这两个的存储布局将不相同。

This is not a requirement of the standard. A conforming implementation could insert additional information or padding such that sizeof(array<T>) is larger than sizeof(T[]) for some T. If it did then the storage layout for these two would not be the same.

这篇关于在c样式数组上的std :: array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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