C ++條件運算符與if-else

[英]C++ Conditional Operator versus if-else


I have always wondered about this. Let's say we have a variable, string weight, and an input variable, int mode, which can be 1 or 0.

我一直都在想這個。假設我們有一個變量,字符串權重和一個輸入變量int模式,它可以是1或0。

Is there a clear benefit to using:

使用有明顯的好處:

weight = (mode == 1) ? "mode:1" : "mode:0";

over

if(mode == 1)
    weight = "mode:1";
else
    weight = "mode:0";

beyond code readability? Are speeds at all affected, is this handled differently by the compiler (such as the ability of certain switch statements to be converted to jump tables)?

超出代碼可讀性?是否所有受影響的速度都由編譯器處理(例如某些switch語句轉換為跳轉表的能力)?

3 个解决方案

#1


3  

The key difference between the conditional operator and an if/else block is that the conditional operator is an expression, rather than a statement. Thus, there are few places you can use the conditional operator where you can't use an if/else. For example, initialization of constant objects, like so:

條件運算符和if / else塊之間的關鍵區別在於條件運算符是表達式而不是語句。因此,您可以使用條件運算符的地方很少,您不能使用if / else。例如,常量對象的初始化,如下所示:

const double biasFactor = (x < 5) ? 2.5 : 6.432;

If you used if/else in this case, biasFactor would have to be non-const.

如果在這種情況下使用if / else,則biasFactor必須是非const。

Additonally, constructor initializer lists call for expressions rather than statements as well:

另外,構造函數初始化器列表也調用表達式而不是語句:

X::X()
  : myData(x > 5 ? 0xCAFEBABE : OxDEADBEEF)
{
}

In this case, myData may not have any assignment operator or non-const member functions defined--its constructor may be the only way to pass any parameters to it.

在這種情況下,myData可能沒有定義任何賦值運算符或非const成員函數 - 它的構造函數可能是將任何參數傳遞給它的唯一方法。

Also, note that any expression can be turned into a statement by adding a semicolon at the end--the reverse is not true.

另外,請注意,任何表達式都可以通過在末尾添加分號來轉換為語句 - 反之則不然。

#2


1  

No, this is purely about presenting the code to a human reader. I'd expect any compiler to generate identical code for these.

不,這純粹是為了向人類讀者呈現代碼。我希望任何編譯器都能為這些生成相同的代碼。

#3


0  

With mingw, the assembly code generated with

使用mingw,生成匯編代碼

const char *  testFunc()
{
    int mode=1;
    const char * weight = (mode == 1)? "mode:1" : "mode:0";
    return weight;
}

is:

testFunc():
0040138c:   push %ebp
0040138d:   mov %esp,%ebp
0040138f:   sub $0x10,%esp
10          int mode=1;
00401392:   movl $0x1,-0x4(%ebp)
11          const char * weight = (mode == 1)? "mode:1" : "mode:0";
00401399:   cmpl $0x1,-0x4(%ebp)
0040139d:   jne 0x4013a6 <testFunc()+26>
0040139f:   mov $0x403064,%eax
004013a4:   jmp 0x4013ab <testFunc()+31>
004013a6:   mov $0x40306b,%eax
004013ab:   mov %eax,-0x8(%ebp)
12          return weight;
004013ae:   mov -0x8(%ebp),%eax
13        }

And with

const char *  testFunc()
{
    const char * weight;
    int mode=1;
    if(mode == 1)
        weight = "mode:1";
    else
        weight = "mode:0";

    return weight;
}

is:

testFunc():
0040138c:   push %ebp
0040138d:   mov %esp,%ebp
0040138f:   sub $0x10,%esp
11          int mode=1;
00401392:   movl $0x1,-0x8(%ebp)
12          if(mode == 1)
00401399:   cmpl $0x1,-0x8(%ebp)
0040139d:   jne 0x4013a8 <testFunc()+28>
13              weight = "mode:1";
0040139f:   movl $0x403064,-0x4(%ebp)
004013a6:   jmp 0x4013af <testFunc()+35>
15              weight = "mode:0";
004013a8:   movl $0x40306b,-0x4(%ebp)
17          return weight;
004013af:   mov -0x4(%ebp),%eax
18        }

Pretty much the same code is generated. The performance of your application shouldn't depend on small details like this one.

幾乎相同的代碼生成。您的應用程序的性能不應該依賴於像這樣的小細節。

So, no it doesn't make a difference.

所以,不,它沒有任何區別。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/11/24/658a963d7b69c936464dae07333616cd.html



 
  © 2014-2022 ITdaan.com 联系我们: