【程式語言】What's new in C++17

Structured Bindings

1
auto [a, b, c] = tuple('a', 1, 0.5);
1
2
3
4
5
map<int, int> mymap = {{1, 2}, {3, 4}};

for (const auto& [key, value] : mymap) {
// ...
}
1
2
3
for (auto [a, b] = tuple(0, 'a'); a < 5; a++) {
// ...
}

Template Argument Deduction

c++17
1
auto p = pair(2, 4.5);
c++14
1
auto p = pair<int, double>(2, 4.5);

Selection Initialization

1
2
3
if (auto a = getval(); a < 10) {
// ...
}
1
2
3
switch (auto ch = getnext(); ch) {
// ...
}

  1. https://www.fluentcpp.com/2018/06/19/3-simple-c17-features-that-will-make-your-code-simpler/
  2. https://hackernoon.com/a-tour-of-c-17-if-constexpr-3ea62f62ff65
  3. https://stackoverflow.com/questions/38060436/what-are-the-new-features-in-c17
Read more