1

I was writing some code in C++ with templates, and there was a parameter like uint16_t >> uint16_t, but the compiler deduced T = int.

There is also an example:

#include <cstdint>
template<class Type> struct S;
void foo() {
    S<decltype(uint16_t(1) >> uint16_t(1))>();
}

Where the compiler (x86-64 gcc 10.3) gave

<source>: In function 'void foo()':
<source>:4:45: error: invalid use of incomplete type 'struct S<int>'
    4 |     S<decltype(uint16_t(1) >> uint16_t(1))>();
      |                                             

But why is uint16_t(1) >> uint16_t(1) an int?

(I learnt this method of getting the type in this post)

XuanInsr
  • 119
  • 6
  • 2
    Because shifting is an arthmetic operation and promotion to `int` happens. – Jason Aug 29 '22 at 07:37
  • I'm clear now! [Integral promotion](https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion) says: arithmetic operators do not accept types smaller than `int` as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. Thanks for marking the duplicate! – XuanInsr Aug 29 '22 at 09:11
  • And just to add to the confusion, if the compiler uses 16-bit `int`s there will be no conversion. – Pete Becker Aug 29 '22 at 13:59

0 Answers0