C++ Logo

std-discussion

Advanced search

Validity of conversions between 4 byte-like types (byte, char) objects

From: Roman Babinicz <rb_at_[hidden]>
Date: Tue, 10 Dec 2019 18:31:41 +0100
What is the validity of code that does conversions between the 4
byte-like types (byte and 3 char variants)?

I've written what I gathered from standard that they are in C++14 and C++17.
Also noted the case for C++20 (which might change, is not standard as of
time of writing).


Please correct this summary if there are any mistakes.


The case is creation an object of one type from object of another type,
as in expression
  unsigned char B = A; // <---
where A is variable of type e.g. std::byte.
We make no assumptions on the values in source variable A.



Object byte from object byte: is same type (allowed - defined happens)
Object byte from object unsigned-char: is defined (byte will get the bit
pattern of normal binary notation of positive integers).
Object byte from object signed-char: is impl.def. until C++20, later
defined as 2s complement bit pattern
Object byte from object char: until C++20 is impl.def. because might be
as signed-char, later it is defined.

Object unsigned-char from object byte: defined (bit pattern of normal
binary for positive integers)
Object unsigned-char from object unsigned-char: same type.
Object unsigned-char from object signed-char: impl.def. because it is
modulo-2N of the bit-pattern that represents the signed, but pattern is
unknown; how ever since C++20 the pattern is known as 2s-complement so
it is fully defined
Object unsigned-char from object char: impl.def until C++20 since it
might be signed char; since C++20 fully defined in both cases

Object char from object byte: impl-defined because target might be
signed char, and the bit patterns for negative are impl.defined; Since
C++20 fully defined.
Object char from object unsigned-char: same as from byte
Object char from object signed-char: impl.def till C++20; since C++20
fully defined
Object char from object char: same type.

Object signed char from object byte: impl.def. till C++20, later defined
Object signed char from object unsigned-char: same as from byte
Object signed char from object signed-char: same type
Object signed char from object char: impl.def. till C++20, later defined

Received on 2019-12-10 11:34:09