Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer literal for fixed width integer types

Tags:

c++

c++11

Is there some c++ proposal for Integer literal for fixed width integer types like this?

// i's type is unsigned int
auto i = 10u;
// j's type is uint32_t
auto j = 10u32;
like image 745
康桓瑋 Avatar asked Dec 03 '20 06:12

康桓瑋


People also ask

What are fixed width integer types?

The fixed-width integer types that <inttypes. h> provides, include signed integer types, such as int8_t, int16_t, int32_t, int64_t, and unsigned integer types, such as uint8_t, uint16_t, uint32_t, and uint64_t.

What does int64_t mean?

A long on some systems is 32 bits (same as an integer), the int64_t is defined as a 64 bit integer on all systems (otherwise known as a long long). Portability may be affected using long, but using int64_t looks like it was created to increase portability.


1 Answers

Yes: P1280R0 Integer Width Literals (published 2018-10-05).

It proposes the following literals:

namespace std::inline literals::inline integer_literals {
  constexpr uint64_t operator ""u64 (unsigned long long arg);
  constexpr uint32_t operator ""u32 (unsigned long long arg);
  constexpr uint16_t operator ""u16 (unsigned long long arg);
  constexpr uint8_t operator ""u8 (unsigned long long arg);

  constexpr int64_t operator ""i64 (unsigned long long arg);
  constexpr int32_t operator ""i32 (unsigned long long arg);
  constexpr int16_t operator ""i16 (unsigned long long arg);
  constexpr int8_t operator ""i8 (unsigned long long arg);
}

And its update P1280R1 that "Modifies return types to actually be [u]int_leastXX_t and friends. This is to make sure that we are actually replacing the [U]INTxx_C macros, as these return a [u]int_leastXX_t":

namespace std::inline literals::inline integer_literals {
  constexpr uint_least64_t operator ""u64 (unsigned long long arg);
  constexpr uint_least32_t operator ""u32 (unsigned long long arg);
  constexpr uint_least16_t operator ""u16 (unsigned long long arg);
  constexpr uint_least8_t operator ""u8 (unsigned long long arg);

  constexpr int_least64_t operator ""i64 (unsigned long long arg);
  constexpr int_least32_t operator ""i32 (unsigned long long arg);
  constexpr int_least16_t operator ""i16 (unsigned long long arg);
  constexpr int_least8_t operator ""i8 (unsigned long long arg);
}

There is a 2019-06-12 update P1280R2 that makes the literals consteval instead of constexpr.

like image 180
bolov Avatar answered Oct 02 '22 07:10

bolov