1

Why does this static assertion fail?

static_assert(std::is_same<uint16_t, decltype(uint16_t(0)|uint16_t(0))>::value);

According to the Bitwise logic operators section of the cppreference page on Arithmetic operators:

Usual arithmetic conversions are performed on both operands and determine the type of the result."

Shouldn't this mean the bitwise OR of two unsigned shorts should be an unsigned short?

John Gordon
  • 2,576
  • 3
  • 24
  • 29
  • They should be `int` – Ted Lyngmo May 29 '23 at 12:40
  • 1
    Integral promotion converts `unsigned short` to `int`. – john May 29 '23 at 12:41
  • Just before that bit, "[...] before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes [integral promotion](https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion)" – molbdnilo May 29 '23 at 12:43
  • To quote [cppreference](https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion) 'In particular, arithmetic operators do not accept types smaller than int as arguments,' – john May 29 '23 at 12:43
  • 1
    Wow! Thanks. Not sure how I never noticed that before. – John Gordon May 29 '23 at 12:46

0 Answers0