2016-07-29 66 views
6

我無法找到任何信息的新的C++ 17如果初始化語法 和 'constexpr如果' 中:如果使用標準保證的初始化程序,是否是constexpr? 'constexpr(constexpr自動X = F(); x)的{}'

http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html

儘管如此,語法由鏘頭支持...

constexpr auto f() { return true; } 
int main() { 
    if constexpr(constexpr auto x = f(); x) { } 
} 
這裏

在線代碼 - >http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr

是在constexpr if與初始值設定器保證的標準,因爲constexpr if只是一個「ifconstexpr」或它不能保證,必須顯式添加到標準?

+0

在EWG中討論了我們是否需要這個,關於Boost.Hana風格元編程的觀點在同時使用這兩個特性方面得到了極大的改善(相對於只有'if ),因此一致認爲我們需要它(幾乎每個人都同意這一點,反對一票)。 – Griwes

回答

7

Selection statements with initializer提案提到if constexpr,並聲明「if constexpr的設施與此提案中的擴展if聲明一樣正常工作」。

關於if聲明的規範在初始化程序N4606 [stmt.if] p3中明確允許使用if constexpr

這裏是N4606 [stmt.if] P3說:

的,如果表單

if constexpr[opt] (init-statement condition) statement 

的語句相當於

{ 
    init-statement 
    if constexpr[opt] (condition) statement 
} 

和如果的聲明表格

if constexpr[opt] (init-statement condition) statement else statement 

相當於

{ 
    init-statement 
    if constexpr[opt] (condition) statement else statement 
} 

只是在初始化語句中聲明的名字都在同一個聲明區域那些在條件聲明。

+0

很好的答案,但與N4606的鏈接已斷開 – Alejandro