0%

Some brief thought about C++17

根據這篇這篇,C++17的最終定案即將出爐。之前喊得狒狒洋洋的moduleconceptcoroutine沒有一個列為標準配備。相對之前喊得狒狒洋洋的大變動。還真是雷聲大雨點小。
不過還是有些實用的技術可以馬上用到,學習曲線沒那麼高..
不過這年頭每個程式語言都在新增新特性,像C語言這種實在難能可貴。不過C太低階了,開發大程式實在很麻煩。真是兩難

Structured bindings

之前說過,C++沒有return multiple value的機制,要做出類似的小果只能這麼做。

1
2
3
4
std::tuple<bool, int> getValue();
bool retB;
int retInt;
std::tie(retB, retInt) = getValue();

可以用,不過不太美觀。到了C++17有了更直觀的方法

1
auto [retB, retInt] = getValue();

這樣就很像go的語法了。更多的討論可以看Returning multiple values from functions in C++

If statement with initializer

這也是另一個我覺得很棒的點
直接看範例,為進化前

1
2
3
4
5
6
7
8
9
status_code foo() { 
{
status_code c = bar();
if (c != SUCCESS) {
return c;
}
}
// ...
}

進化後,可能的情況

1
2
3
4
5
6
status_code foo() {
if (status_code c = bar(); c != SUCCESS) {
return c;
}
// ...
}

希望這兩點未來C語言也會加回去啊