久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++,'if' 表達式中的變量聲明

C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
本文介紹了C++,'if' 表達式中的變量聲明的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是怎么回事?

if(int a = Func1())
{
    // Works.
}

if((int a = Func1()))
{
    // Fails to compile.
}

if((int a = Func1())
    && (int b = Func2()))
)
{
    // Do stuff with a and b.
    // This is what I'd really like to be able to do.
}

2003 標準中的第 6.4.3 節解釋了在選擇語句條件中聲明的變量如何具有擴展到由條件控制的子語句末尾的范圍.但我沒有看到它在哪里說明不能在聲明周圍加上括號,也沒有說明每個條件只有一個聲明.

Section 6.4.3 in the 2003 standard explains how variables declared in a selection statement condition have scope that extends to the end of the substatements controlled by the condition. But I don't see where it says anything about not being able to put parenthesis around the declaration, nor does it say anything about only one declaration per condition.

即使在條件中只需要一個聲明的情況下,這個限制也很煩人.考慮一下.

This limitation is annoying even in cases where only one declaration in the condition is required. Consider this.

bool a = false, b = true;

if(bool x = a || b)
{

}

如果我想在 x 設置為 false 的情況下進入 'if'-body 范圍,則聲明需要括號(因為賦值運算符的優先級低于邏輯 OR),但由于不能使用括號,因此需要聲明的 x 體外,將該聲明泄漏到比預期更大的范圍.顯然這個例子是微不足道的,但更現實的情況是 a 和 b 是返回需要測試的值的函數

If I want to enter the 'if'-body scope with x set to false then the declaration needs parenthesis (since the assignment operator has lower precedence than the logical OR), but since parenthesis can't be used it requires declaration of x outside the body, leaking that declaration to a greater scope than is desired. Obviously this example is trivial but a more realistic case would be one where a and b are functions returning values that need to be tested

那么我想做的事情不符合標準,還是我的編譯器只是在破壞我的球(VS2008)?

So is what I want to do non-conformant to the standard, or is my compiler just busting my balls (VS2008)?

推薦答案

從 C++17 開始,您想要做什么 終于有可能:

As of C++17 what you were trying to do is finally possible:

if (int a = Func1(), b = Func2(); a && b)
{
    // Do stuff with a and b.
}

注意使用;而不是來分隔聲明和實際情況.

Note the use of ; of instead of , to separate the declaration and the actual condition.

這篇關于C++,'if' 表達式中的變量聲明的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
if (cin gt;gt; x) - Why can you use that condition?(if (cin x) - 為什么你可以使用那個條件?)
主站蜘蛛池模板: 爱搞逼综合网 | 四虎影院免费观看 | 四虎在线观看 | 亚洲第一黄色 | 久久国产精 | 丁香婷婷在线 | 欧美一区二区三区视频 | 国产精品av一区二区 | 亚洲爽妇网 | 91资源在线观看 | 免费成人在线观看视频 | 日韩中文字幕在线观看 | 欧美成人一级 | 国产精品二区一区二区aⅴ污介绍 | 五月婷婷综合网 | 亚洲免费观看 | 免费av不卡| av一区二区三区在线观看 | 中文字幕一区二区三区视频 | 日韩影音 | 国产一区中文字幕 | 国产成人精品一区二区三区在线观看 | 又色又爽又黄gif动态图 | 日韩成人在线播放 | a视频在线免费观看 | 中文字幕在线日韩 | 国产激情小视频 | 五月天一区二区三区 | 91伦理| 欧美一级淫片bbb一84 | 久久久精品在线观看 | 人人爽夜夜爽 | 日韩1级片 | 日韩国产一区二区三区 | 亚洲视频网 | 亚洲天天看| 中文在线观看免费视频 | 9l视频自拍九色9l视频成人 | 国产乱码一区二区 | 亚洲精品无 | 国产乱人乱偷精品视频 |