組み込み型の C++ 二項演算子では、両方のオペランドが同じ型を持つ必要があります。そうでない場合、階層に基づいてオペランドの 1 つが他のオペランドの型に変換されます。
long double
double
float
unsigned long long int
long long int
unsigned long int
long int
unsigned int
int
私の質問は次unsigned T
のとおりですT
。それは単なる恣意的な選択ですか、それとも変換することにはいくつかの利点がありT
、Unsigned T
その逆ではありません.
アップデート:
//where `unsigned int` to `int` work better.
int a=-3;
unsigned int b=3;
cout << a+b; /* this will output the right result if b get converted to int,
which is not what really happen.*/
//where `int` to `unsigned int` work better.
int a=3;
unsigned int b=pow(2,20);
cout << a+b; /* this will output the right result if a get converted to unsigned int,
which is fortunately what really happen.*/
ですから、会話T
することUnsigned T
が他の方法よりも多くの利点があるとは思いません。