在阅读了不少关于联合体用途和类型双关(基本上是不允许的,你应该依赖编译器优化 memcpy 调用)的文章后,我想知道以下联合体用法是否完全不安全,或者它是否完全是未定义行为,或者它实际上是否完全符合联合体规范且安全。其理念是,底层数据都属于相同的基本类型(int、double 等),但我只是想看看它是如何以不同的方式布局或命名的。我或许应该称之为“容器双关”,而不是“类型双关”。
// main.cpp
// compile with `c++ main.cpp -std=c+23`
#include <iostream>
#include <array>
#include <format>
struct Mat2 {
union {
std::array<float, 4> data;
std::array<std::array<float, 2>, 2> rows;
struct { float x, y, z, w; };
};
};
int main() {
// sometimes you want to think about it in rows
Mat2 m{ .rows = {{ {0, 1},
{2, 3} }}};
for (auto &row : m.rows) {
for (auto &col : row) {
std::cout << col << " ";
}
std::cout << "\n";
}
// sometimes you want to think about it as a block of data
std::cout << "\n";
for (auto &d : m.data) {
std::cout << d << " ";
}
std::cout << "\n\n";
// sometimes you want to access elements based on sematic names
std::cout << std::format("{}, {}, {}, {}", m.x, m.y, m.z, m.w);
std::cout << std::endl;
}