27

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;
2
  • 1
    Related: stackoverflow.com/questions/36406333/… Commented Dec 3, 2020 at 6:12
  • 1
    The old C99 way is to use macros in stdint.h - for example: INT8_C(127), UINT16_C(65535), UINT32_C(10). Commented Dec 3, 2020 at 15:02

1 Answer 1

31

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.


Status update

I found this issue tracking the paper: https://github.com/cplusplus/papers/issues/153

The latest update is

Jul 19, 2019

Discussed in LEWG in Cologne - http://wiki.edg.com/bin/view/Wg21cologne2019/P1280

Forward to LWG for C++23.

SF F N A SA
0 0 3 7 4

Dropping, barring additional revision from the author discussing the points raised in the discussion.

the table represents number of votes: (strongly) for / neutral / (strongly) against

And in this Cologne 2019 LEWG Summary the resolution of the paper is "Discussed but not Approved nor Forwarded"

Sign up to request clarification or add additional context in comments.

10 Comments

fwiw, I think that update made it worse. If I write auto x = 5u32; I want a 32bit integer and not "maybe something else that will fail on other platform"
@RiaD -- it's uint32_t that will fail on some other platform. uint_least32_t is always available; uint32_t won't exist on (admittedly unusual) platforms that don't have a native 32-bit integer type. That's an inherent ambiguity with fixed-size integer types: you might want an exact size, or you might want something large enough to hold some number of bits.
@PeteBecker 1) "platform doesn't support uint32, so I won't compile 5u32" is straightforward compiler error 2) "in some random place where uint_least32_t is used, some overloading is now ambiguous" (suppose that uint32_least_t=unsigned long long and the same example will be broken as in P1280) is less straight forward. I of course agree that you might want uint32_least_t, but why not use 5u_least32 ?
And does multiplying two large uint16_ts and storing the result in a uint16_t variable result in wraparound or undefined behavior? Integer math in C/C++ is a horrible mess.
@MarkRansom update on the answer. Sadly it looks like it's dropped
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.