Casting int to bool in C/C++

前端 未结 3 899
轮回少年
轮回少年 2020-12-03 02:29

I know that in C and C++, when casting bools to ints, (int)true == 1 and (int)false == 0. I\'m wondering about casting in the reverse direction...<

3条回答
  •  隐瞒了意图╮
    2020-12-03 03:21

    There some kind of old school 'Marxismic' way to the cast int -> bool without C4800 warnings of Microsoft's cl compiler - is to use negation of negation.

    int  i  = 0;
    bool bi = !!i;
    
    int  j  = 1;
    bool bj = !!j;
    

提交回复
热议问题