考虑到整个 C++11 标准,任何符合要求的实现是否有可能成功下面的第一个断言但失败了后者?
#include <cassert>
int main(int, char**)
{
const int I = 5, J = 4, K = 3;
const int N = I * J * K;
int arr1d[N] = {0};
int (&arr3d)[I][J][K] = reinterpret_cast<int (&)[I][J][K]>(arr1d);
assert(static_cast<void*>(arr1d) ==
static_cast<void*>(arr3d)); // is this necessary?
arr3d[3][2][1] = 1;
assert(arr1d[3 * (J * K) + 2 * K + 1] == 1); // UB?
}
如果不是,这在技术上是否是 UB,如果第一个断言被删除,答案是否会改变(reinterpret_cast
是否保证在这里保留地址?)?另外,如果 reshape 是在相反的方向(3d 到 1d)或从 6x35 阵列到 10x21 阵列进行的?
编辑:如果答案是由于 reinterpret_cast
而导致这是 UB,是否还有其他一些严格兼容的 reshape 方式(例如,通过 static_cast
到/从中间 void *
)?
最佳答案
2021-03-20 更新:
同样的问题是 asked on Reddit最近有人指出我的原始答案有缺陷,因为它没有考虑到这个 aliasing rule :
If a program attempts to access the stored value of an object through a glvalue whose type is not similar to one of the following types the behavior is undefined:
- the dynamic type of the object,
- a type that is the signed or unsigned type corresponding to the dynamic type of the object, or
- a char, unsigned char, or std::byte type.
根据 similarity 的规则,这两种数组类型在上述任何情况下都不相似,因此通过 3D 数组访问 1D 数组在技术上是未定义的行为。 (这绝对是其中一种情况,在实践中,它几乎可以肯定适用于大多数编译器/目标)
请注意,原始答案中的引用是指较旧的 C++11 草案标准
原答案:
reinterpret_cast
引用文献
标准规定 T1
类型的左值可以是reinterpret_cast
对 T2
的引用如果指向 T1
的指针可以是reinterpret_cast
指向 T2
的指针(§5.2.10/11):
An lvalue expression of type
T1
can be cast to the type “reference toT2
” if an expression of type “pointer toT1
” can be explicitly converted to the type “pointer toT2
” using a reinterpret_cast.
所以我们需要确定 int(*)[N]
可以转换为 int(*)[I][J][K]
.
reinterpret_cast
指针
指向 T1
的指针可以是reinterpret_cast
指向 T2
的指针如果两者都是 T1
和 T2
是标准布局类型和 T2
没有比 T1
更严格的对齐要求(§5.2.10/7):
When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is
static_cast<cv T2*>(static_cast<cv void*>(v))
if bothT1
andT2
are standard-layout types (3.9) and the alignment requirements ofT2
are no stricter than those ofT1
, or if either type is void.
是
int[N]
和int[I][J][K]
标准布局类型?int
是标量类型,标量类型的数组被视为标准布局类型(第 3.9/9 节)。Scalar types, standard-layout class types (Clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called standard-layout types.
是否
int[I][J][K]
没有比int[N]
更严格的对齐要求.alignof
的结果运算符给出完整对象类型的对齐要求(第 3.11/2 节)。The result of the
alignof
operator reflects the alignment requirement of the type in the complete-object case.由于这里的两个数组不是任何其他对象的子对象,它们是完整的对象。申请
alignof
到数组给出元素类型的对齐要求(§5.3.6/3):When
alignof
is applied to an array type, the result shall be the alignment of the element type.所以这两种数组类型具有相同的对齐要求。
这使得 reinterpret_cast
有效且等价于:
int (&arr3d)[I][J][K] = *reinterpret_cast<int (*)[I][J][K]>(&arr1d);
在哪里 *
和 &
是内置运算符,则相当于:
int (&arr3d)[I][J][K] = *static_cast<int (*)[I][J][K]>(static_cast<void*>(&arr1d));
static_cast
通过void*
static_cast
至void*
标准转换(§4.10/2)允许:
A prvalue of type “pointer to cv
T
,” whereT
is an object type, can be converted to a prvalue of type “pointer to cv void”. The result of converting a “pointer to cvT
” to a “pointer to cv void” points to the start of the storage location where the object of typeT
resides, as if the object is a most derived object (1.8) of typeT
(that is, not a base class subobject).
static_cast
至int(*)[I][J][K]
然后被允许(§5.2.9/13):
A prvalue of type “pointer to cv1
void
” can be converted to a prvalue of type “pointer to cv2T
,” whereT
is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.
所以转换没问题!但是我们可以通过新的数组引用访问对象吗?
访问数组元素
对像 arr3d[E2]
这样的数组执行数组下标相当于 *((E1)+(E2))
(§5.2.1/1)。让我们考虑以下数组下标:
arr3d[3][2][1]
首先,arr3d[3]
相当于 *((arr3d)+(3))
.左值 arr3d
进行数组到指针的转换,得到 int(*)[2][1]
.不要求底层数组必须是正确的类型才能进行此转换。然后访问指针值(第 3.10 节很好),然后将值 3 添加到它。这种指针算法也很好(§5.7/5):
If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
这个 this 指针被取消引用以给出 int[2][1]
.这对接下来的两个下标进行相同的过程,产生最终的 int
在适当的数组索引处的左值。由于 *
的结果,它是一个左值(§5.3.1/1):
The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points.
这样就可以访问实际的 int
对象通过这个左值,因为左值的类型是 int
太(§3.10/10):
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
- the dynamic type of the object
- [...]
所以除非我错过了什么。我会说这个程序定义明确。
关于c++ - 将一维数组 reshape 为多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283523/