Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are int8_t and uint8_t intended to be char types?

Given this C++11 program, should I expect to see a number or a letter? Or not make expectations?

#include <cstdint> #include <iostream>  int main() {     int8_t i = 65;     std::cout << i; } 

Does the standard specify whether this type can or will be a character type?

like image 975
Drew Dormann Avatar asked Apr 09 '13 20:04

Drew Dormann


People also ask

Is uint8_t same as char?

uint8_t always matches range and size of unsigned char and padding (none) when unsigned char is 8-bit. When unsigned char is not 8-bit, uint8_t does not exist.

What is int8_t and uint8_t?

uint8_t and int8_t are optional integer types of exactly 8 bits. Both types have no padding, and int8_t uses 2's complement. uint8_t is unsigned, and has the range zero to UINT8_MAX , which is [0, +255]. int8_t is signed, and has the range INT8_MIN to INT8_MAX , which is [−128, +127].

Is uint8_t always unsigned char?

If it exists, uint8_t must always have the same width as unsigned char . However, it need not be the same type; it may be a distinct extended integer type. It also need not have the same representation as unsigned char ; for instance, the bits could be interpreted in the opposite order.

Can you compare int8_t and uint8_t?

An integral promotion for uint8_t and int8_t is possible to int , so it is obligatorily applied. Therefore the comparison between a uint8_t and int8_t is transformed by the compiler into a comparison between 2 int . There is no undeterministic behaviour.


1 Answers

From § 18.4.1 [cstdint.syn] of the C++0x FDIS (N3290), int8_t is an optional typedef that is specified as follows:

namespace std {   typedef signed integer type int8_t;  // optional   //... } // namespace std 

§ 3.9.1 [basic.fundamental] states:

There are five standard signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

...

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.

§ 3.9.1 also states:

In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

It is tempting to conclude that int8_t may be a typedef of char provided char objects take on signed values; however, this is not the case as char is not among the list of signed integer types (standard and possibly extended signed integer types). See also Stephan T. Lavavej's comments on std::make_unsigned and std::make_signed.

Therefore, either int8_t is a typedef of signed char or it is an extended signed integer type whose objects occupy exactly 8 bits of storage.

To answer your question, though, you should not make assumptions. Because functions of both forms x.operator<<(y) and operator<<(x,y) have been defined, § 13.5.3 [over.binary] says that we refer to § 13.3.1.2 [over.match.oper] to determine the interpretation of std::cout << i. § 13.3.1.2 in turn says that the implementation selects from the set of candidate functions according to § 13.3.2 and § 13.3.3. We then look to § 13.3.3.2 [over.ics.rank] to determine that:

  • The template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char) template would be called if int8_t is an Exact Match for signed char (i.e. a typedef of signed char).
  • Otherwise, the int8_t would be promoted to int and the basic_ostream<charT,traits>& operator<<(int n) member function would be called.

In the case of std::cout << u for u a uint8_t object:

  • The template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char) template would be called if uint8_t is an Exact Match for unsigned char.
  • Otherwise, since int can represent all uint8_t values, the uint8_t would be promoted to int and the basic_ostream<charT,traits>& operator<<(int n) member function would be called.

If you always want to print a character, the safest and most clear option is:

std::cout << static_cast<signed char>(i); 

And if you always want to print a number:

std::cout << static_cast<int>(i); 
like image 187
Daniel Trebbien Avatar answered Sep 23 '22 17:09

Daniel Trebbien