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

      <legend id='WEkF7'><style id='WEkF7'><dir id='WEkF7'><q id='WEkF7'></q></dir></style></legend>
    1. <small id='WEkF7'></small><noframes id='WEkF7'>

    2. <tfoot id='WEkF7'></tfoot>

      • <bdo id='WEkF7'></bdo><ul id='WEkF7'></ul>

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

      Is !! a safe way to convert to bool in C++?(是 !!在 C++ 中转换为 bool 的安全方法?)
        <bdo id='ntIob'></bdo><ul id='ntIob'></ul>

          <legend id='ntIob'><style id='ntIob'><dir id='ntIob'><q id='ntIob'></q></dir></style></legend>
        • <small id='ntIob'></small><noframes id='ntIob'>

        • <tfoot id='ntIob'></tfoot>

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

                <tbody id='ntIob'></tbody>
                本文介绍了是 !!在 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 的安全方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <tbody id='XlOjO'></tbody>

                • <small id='XlOjO'></small><noframes id='XlOjO'>

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