我是因为this question才知道这个的,标准规定为 std::complex
(26.4 [复数]):
4 If
z
is an lvalue expression of type cvstd::complex<T>
then:
— the expressionreinterpret_cast<cv T(&)[2]>(z)
shall be well-formed,
—reinterpret_cast<cv T(&)[2]>(z)[0]
shall designate the real part ofz
, and
—reinterpret_cast<cv T(&)[2]>(z)[1]
shall designate the imaginary part ofz
.
Moreover, ifa
is an expression of type cvstd::complex<T>*
and the expressiona[i]
is well-defined for an integer expressioni
, then:
—reinterpret_cast<cv T*>(a)[2*i]
shall designate the real part ofa[i]
, and
—reinterpret_cast<cv T*>(a)[2*i + 1]
shall designate the imaginary part ofa[i]
.
这是我真正想要以符合标准的方式利用的东西。有时我有 POD,比如数学 vector ,它们由单一数据类型组成。下面是两个示例类:
template <typename T, unsigned N>
struct Vector
{
T v[N];
};
template <typename T>
struct Quaternion
{
T r, i, j, k;
};
据我了解,允许在最后一个成员之后以及成员之间添加填充。这意味着 sizeof(Quaterntion<float>)
可能不等于 sizeof(float[4])
, 和 sizeof(Vector<double, 8>)
可能不等于 sizeof(double[8])
.这意味着我通常必须添加一些 static_assert
s 到我的代码以确保我可以转换我的 Vector<float, N>
/Quaterntion<float>
到 float*
,例如,不用担心填充(例如,为了传递给 C 库或 OpenGL 缓冲区)。
是否有标准提供的一些方法可以让我对我的小 POD 有相同的保证,比如 Vector
和 Quaternion
, 作为 std::complex
做?我知道特定于实现的事情,比如 __attribute__((packed))
.我正在寻找一种非特定于实现的、符合标准的方法来执行此操作。由于标准要求为提供 std::complex
的实现支持此类事物,我想知道是否还有一些标准方法可以将此保证应用于我自己的类(class)。
最佳答案
我认为你在问不可能的事情。
请记住,标准库实现者通常依赖于非标准扩展或实现定义的行为。事实上,在 VC++ 的复杂头文件中我们发现:
#pragma pack(push, _CRT_PACKING)
// implementation
#pragma pack(pop)
您可以为四元数做的是将所有成员放在一个数组中,因为结构地址可以重新解释为指向第一个成员的指针。但我想这违背了结构的目的(通过名称直接访问成员)。
这不是你所要求的,而是提供一个
operator const T*() const // can be written in a portable manner
对于你的结构,将允许你写
Quaternion<double> q = {};
const double * p = q;
以额外的运行时/内存开销为代价,具体取决于您如何实现转换运算符。
关于c++ - 我怎样才能让我的对象重新解释为数组,比如 std::complex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22925905/