C ++ 17可在大端平台上实现吗? [英] Is C++17 implementable on big-endian platforms?

查看:79
本文介绍了C ++ 17可在大端平台上实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看下面的代码:

int i = 10;
char c = reinterpret_cast<char&>(i);

[expr.reinterpret.cast]/11 :

如果可以使用reinterpret_cast将类型指向T1的指针"显式转换为类型指向T2的指针",则可以将类型T1的glvalue表达式强制转换为对T2的引用"类型.结果指向与源glvalue相同的对象,但是具有指定的类型.

A glvalue expression of type T1 can be cast to the type "reference to T2" if an expression of type "pointer to T1" can be explicitly converted to the type "pointer to T2" using a reinterpret_­cast. The result refers to the same object as the source glvalue, but with the specified type.

因此具有指定 char 类型的 reinterpret_cast< char&>(i)左值是指 int 对象 i.

So the reinterpret_cast<char&>(i) lvalue with the specified char type refers to the int object i.

要初始化 c ,我们需要值,因此要应用从左值到右值的转换

To initialize c, we need value, so the lvalue-to-rvalue conversion is applied [conv.lval]/3.4:

glvalue指示的对象中包含的值是prvalue结果.

the value contained in the object indicated by the glvalue is the prvalue result.

L2R转换的结果是 i 对象中包含的值.只要 i 的值在 char (

The result of the L2R conversion is the value contained in the i object. As long as the value of i is in the range representable by char ([expr]/4 says that otherwise this is UB), the variable c shall be initialized to have the same value.

从实现POV来看,在小端平台上,可以通过读取 i 对象地址处的字节来轻松实现.但是,在big-endian平台上,编译器将必须添加偏移量以获取最低有效字节.或者,将整个 int 对象读入寄存器并屏蔽第一个字节,这在两个字节序上都是可以接受的.

From the implementation POV, on a little-endian platform this is easily achievable by reading a byte at the address of i object. However, on a big-endian platform the compiler will have to add an offset to fetch the least significant byte. Or, read the whole int object into a register and mask the first byte, which is acceptable approach on both endians.

如果您认为上面的代码可以很容易地由编译器处理以产生符合C ++ 17标准要求的代码,请考虑将指针转换为指向 int 的指针.> i 到指向 char 的指针.这种强制转换不会更改指针值,即,它仍指向 int 对象 i ,这意味着通过以下L2R转换将间接操作符应用于此类指针将起作用如上所述,即,获取 int 对象的值(如果可以用 char 类型表示).

If you think that the code above could be easily handled by a compiler to produce a code behaving as required by the C++17 Standard, think of casting a pointer to int pointing to i into a pointer to char. Such cast does not change the pointer value, i.e. it still points to the int object i, which means that applying the indirection operator to such pointer with the following L2R conversion shall behave as it was described above, i.e. fetch the value of the int object if it is representable by the char type.

在以下代码中

int i = 10;
f(reinterpret_cast<char*>(&i)); // void f(char*)

如果编译器不知道函数 f 将对其参数执行什么操作,编译器是否应将其偏移量调整 i 的地址?而且,编译器也不知道将传递给函数 f 的内容.上面的代码和函数 f 在不同的翻译单元中.

should the compiler adjust the address of i by some offset, if it does not know what the function f will do with its argument? And also the compiler does not know what will be passed to the function f. The code above and the function f are in different translation units.

例如,如果 f 取消引用指针以通过其读取值,则如上所述,它将获得 i 的值.但是也可以使用指向实际 char 对象的指针来调用它,因此 f 不能调整给定的指针.这意味着调用方应调整指针.但是如果 f 将指针传递给 memcpy 来将 sizeof(int)个字节复制到此大小的字符数组并返回到另一个 int 对象, [basic.types]允许/3 ?很难想象如何在此处调整指针以达到要求(通过 [conv.lval]/3.4 )的行为.

For example, if f dereferences the pointer to read the value through it, it shall get the value of the i, as described above. But it also can be called with a pointer to a real char object, so f can't adjust the given pointer. This means that the caller shall adjust the pointer. But what if f passes the pointer to memcpy to copy sizeof(int) bytes to a character array of this size and back to another int object, as permitted by [basic.types]/3? It is not easy to imagine how to adjust pointers here to mach the required (by both [basic.types]/3 and [conv.lval]/3.4) behavior.

那么,如果有确实符合C ++ 17标准的现有实现,那么现有的实现有什么作用?

So, what existing implementations do, if there are existing implementations really conforming to the C++17 standard?

推荐答案

完全重写:您已经使我确信该标准已被破坏.

Complete rewrite: You've convinced me that the standard is broken.

...结果指向与源glvalue相同的对象,但是具有指定的类型.

... The result refers to the same object as the source glvalue, but with the specified type.


glvalue指示的对象中包含的值是prvalue结果.

the value contained in the object indicated by the glvalue is the prvalue result.

我同意直译可能会得出您得出的结论.

I agree that literal interpretation may lead to the conclusions that you've made.

鉴于您的解释, reinterpret_cast (以及根据 reinterpret_cast 定义的任何内容)变得无用,并且不仅在BE系统上,而且在LE系统上也无法实现(考虑非整数类型和 char 之间的重新解释).因此,我不认为这是预期的含义.这可以被认为是缺陷报告的候选者.

Given your interpretation, reinterpret_cast (and anything defined in terms of reinterpret_cast) becomes useless, and implementation is impossible not only on BE systems, but on LE systems as well (consider reinterpretation between non-integral types and char). As such, I don't believe are the intended meaning. This may be considered as a candidate for a defect report.

混淆可能是由于对表达式包含在指示的对象" 中的值的定义不够准确所致.相同的对象".澄清或改写其中的某些或全部内容可能是有条理的.

The confusion may be due to insufficiently accurate definition for expressions "value contained in", "object indicated" and "The result refers to the same object". Clarifying or rewording some or all of these may be in order.

这篇关于C ++ 17可在大端平台上实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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