lundi 27 mai 2019

Real-world portability of equal-rank unsigned to signed conversion

Take the following code

#include <iostream>

int main() {
    unsigned char i = 128;
    signed char j = i;
    std::cout << static_cast<int>(j) << '\n';
}

compiling on GCC, this produces -128 as most programmers would naturally expect (when assuming CHAR_BIT == 8, which is a safe assumption in 2019). Now, I am fully aware that the initialisation signed char j = i; invokes implementation-defined behaviour in C++ standards before C++20.

My question: Realistically speaking, does anybody know of a semi-interesting compiler implementation of C++ that does not just do the obvious thing (i.e. a no-op in the above case). By semi-interesting, I mean compilers I can realistically encounter, not some prototype Unisys C++ compiler from 1985 for an obscure 43-bit architecture that only the DoD has access to. GCC documents the above behaviour in its documentation, Clang doesn't, but the observable behaviour is the same. My feeling is that there are none, and that this conversion, while formally implementation-defined behaviour, is portable in practice.

Aucun commentaire:

Enregistrer un commentaire