Is !! a safe way to convert to bool in C++?

后端 未结 17 1908
醉梦人生
醉梦人生 2020-11-29 22:25

[This question is related to but not the same as this one.]

If I try to use values of certain types as boolean expressions, I get a warning. Rather than su

17条回答
  •  难免孤独
    2020-11-29 22:51

    The argument of the ! operator and the first argument of the ternary operator are both implicitly converted to bool, so !! and ?: are IMO silly redundant decorations of the cast. I vote for

    b = (t != 0);
    

    No implicit conversions.

提交回复
热议问题