size_t 类型变量 t 赋负值时的实际值是多少?(可以是 unsigned int 而不是 size_t)

what is actual value of variable t of type size_t when it is assigned negative value?(can be unsigned int instead of size_t)

-1 分配给类型 size_t 的变量 t 并检查它与 -1 是否相等和 4294967295FFFFFFFF,2 对 -1 的赞美;我的系统是 64 bit;值可能因系统而异),那么在这两种情况下,它都是 returns 1true

代码是

int main(){
    unsigned int t = 10;
    cout<<"t = -1: "<<(t==-1);  //checks if t is -1
    cout<<"\nt = 4294967295: "<<(t==4294967295);   //checks if t is 4294967295
    cout<<"\nt: "<<t;    //printing actual value of t
    int p = t;    //typecasting it to int
    cout<<"\np: "<<p;    //printing value of p
}

实际输出为

t = -1: 1
t = 4294967295: 1
t: 4294967295
p: -1

返回 1 用于检查 (t==-1)(t==4294697295) 但输出 t = 4294697295 和输出 p = -1。 变量 t 是否持有两个值,即 -14294697295。 绝对不是这样。

需要帮助。 系统内部到底发生了什么??

比较有符号值和无符号值时,有符号值在比较前先转换为无符号值。这是在编译期间完成的。所以 t==-1 变成 t==4294967295ut==4294967295 (有符号整数文字)变成 t==4294967295u.

参考:http://eel.is/c++draft/expr.arith.conv

(1.5.3) Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.