C++11 type of (signed + unsigned)?

c++, c++11, language-lawyer

Solution

What you're seeing are just the effects of the Usual Arithmetic Conversions.

The standard says the following:

`§5 [expr] p7`:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

- [...]

- Otherwise, the integral promotions (4.5) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

- [...]

- Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Problem

``` #include <iostream> #include <typeinfo> using namespace std; int main() { int s = 2; unsigned int u = 3; auto k = s + u; if (typeid(k) == typeid(s)) cout << "signed" << endl; else if (typeid(k) == typeid(u)) cout << "unsigned" << endl; else cout << "error" << endl; } ``` The output of this program by GCC is: ``` unsigned ``` I'm pretty sure this is either undefined or implementation-defined behaviour - but I can't seem to connect the dots with the standard. Can you tell me where in the standard it says this?

Original source