1. <legend id='rxNJR'><style id='rxNJR'><dir id='rxNJR'><q id='rxNJR'></q></dir></style></legend>

      <tfoot id='rxNJR'></tfoot>
    2. <small id='rxNJR'></small><noframes id='rxNJR'>

      • <bdo id='rxNJR'></bdo><ul id='rxNJR'></ul>
      <i id='rxNJR'><tr id='rxNJR'><dt id='rxNJR'><q id='rxNJR'><span id='rxNJR'><b id='rxNJR'><form id='rxNJR'><ins id='rxNJR'></ins><ul id='rxNJR'></ul><sub id='rxNJR'></sub></form><legend id='rxNJR'></legend><bdo id='rxNJR'><pre id='rxNJR'><center id='rxNJR'></center></pre></bdo></b><th id='rxNJR'></th></span></q></dt></tr></i><div id='rxNJR'><tfoot id='rxNJR'></tfoot><dl id='rxNJR'><fieldset id='rxNJR'></fieldset></dl></div>

      是 !!在 C++ 中转换为 bool 的安全方法?

      时间:2023-10-06
          • <bdo id='GAELy'></bdo><ul id='GAELy'></ul>
          • <i id='GAELy'><tr id='GAELy'><dt id='GAELy'><q id='GAELy'><span id='GAELy'><b id='GAELy'><form id='GAELy'><ins id='GAELy'></ins><ul id='GAELy'></ul><sub id='GAELy'></sub></form><legend id='GAELy'></legend><bdo id='GAELy'><pre id='GAELy'><center id='GAELy'></center></pre></bdo></b><th id='GAELy'></th></span></q></dt></tr></i><div id='GAELy'><tfoot id='GAELy'></tfoot><dl id='GAELy'><fieldset id='GAELy'></fieldset></dl></div>
                <tbody id='GAELy'></tbody>

              <small id='GAELy'></small><noframes id='GAELy'>

              <legend id='GAELy'><style id='GAELy'><dir id='GAELy'><q id='GAELy'></q></dir></style></legend>
                <tfoot id='GAELy'></tfoot>
                本文介绍了是 !!在 C++ 中转换为 bool 的安全方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                [这个问题与这个.]

                如果我尝试将某些类型的值用作布尔表达式,则会收到警告.我有时会使用三元运算符 (?:) 来转换为 bool,而不是取消警告.使用两个非运算符 (!!) 似乎做同样的事情.

                If I try to use values of certain types as boolean expressions, I get a warning. Rather than suppress the warning, I sometimes use the ternary operator (?:) to convert to a bool. Using two not operators (!!) seems to do the same thing.

                我的意思是:

                typedef long T;       // similar warning with void * or double
                T t = 0;
                bool b = t;           // performance warning: forcing 'long' value to 'bool'
                b = t ? true : false; // ok
                b = !!t;              // any different?
                

                那么,双重非技术真的能做同样的事情吗?它比三元技术更安全还是更不安全?这种技术对于非整数类型是否同样安全(例如,void *double 用于 T)?

                So, does the double-not technique really do the same thing? Is it any more or less safe than the ternary technique? Is this technique equally safe with non-integral types (e.g., with void * or double for T)?

                我不是在问 !!t 是否是好的风格.我在问它在语义上是否与 t 不同?真:假.

                I'm not asking if !!t is good style. I am asking if it is semantically different than t ? true : false.

                推荐答案

                !运算符和三元运算符的第一个参数都隐式转换为 bool,所以 !!和?:IMO 是演员的愚蠢的多余装饰.我投给

                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);
                

                无隐式转换.

                这篇关于是 !!在 C++ 中转换为 bool 的安全方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Visual C++ 中的 DLL 引用 下一篇:您如何减少 Visual C++ 项目(本机 C++)的编译时间和

                相关文章

                最新文章

              • <i id='nyXhP'><tr id='nyXhP'><dt id='nyXhP'><q id='nyXhP'><span id='nyXhP'><b id='nyXhP'><form id='nyXhP'><ins id='nyXhP'></ins><ul id='nyXhP'></ul><sub id='nyXhP'></sub></form><legend id='nyXhP'></legend><bdo id='nyXhP'><pre id='nyXhP'><center id='nyXhP'></center></pre></bdo></b><th id='nyXhP'></th></span></q></dt></tr></i><div id='nyXhP'><tfoot id='nyXhP'></tfoot><dl id='nyXhP'><fieldset id='nyXhP'></fieldset></dl></div>
                    <bdo id='nyXhP'></bdo><ul id='nyXhP'></ul>

                  <small id='nyXhP'></small><noframes id='nyXhP'>

                1. <tfoot id='nyXhP'></tfoot>

                  1. <legend id='nyXhP'><style id='nyXhP'><dir id='nyXhP'><q id='nyXhP'></q></dir></style></legend>