C++の最新機能
[第]47[課では]テンプレートメタプログラミングについて学びました。
[現在]、[私たちは]C++17[と]C++20[の新機能]を学習します。
[これらの新機能により]C++は[より簡潔]で、[より安全]で、[より強力]になりました。
1. C++17[新機能]
(1) 1.1 [構造化バインディング](Structured Binding)
[機能]: [tuple、pair、structから]簡単に[要素を展開]できる。
例:[構造化バインディング] (難易度 ⭐)
▶ サンプル 2: codeexample (難易度 ⭐)
#include iostream
#include tuple
int main() {
std::tuple<int, std::string, double> t = {1, "Hello", 3.14};
// C++17:bind
auto [id, name, score] = t;
std::cout << "ID: " << id << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Score: " << score << std::endl;
return 0;
}
(2) 1.2 constexpr の場合
[機能]: [条件]に基づいて、[選択的な]コードをコンパイルする。
例:if constexpr (Difficulty ⭐⭐)
#include iostream
#include type_traits
template<typename T>
auto process(T value) {
if constexpr (std::is_integral_vT) {
return value * 2; //:
} else {
return value; //:
}
}
int main() {
std::cout << process(10) << std::endl; // 20
std::cout << process(3.14) << std::endl; // 3.14
return 0;
}
(3) 1.3 [折りたたみ式]
[機能]: [可変引数の簡略化]テンプレート。
例:[折りたたみ式] (難易度 ⭐⭐)
#include iostream
template<typename... Args>
void print(Args... args) {
(std::cout << ... << args) << std::endl; // ...
}
int main() {
print(1, 2, 3, 4, 5); // output:12345
return 0;
}
(4) 1.4 [その他]C++17[機能]
| [特性] | [説明] |
|---|---|
std::optional |
[空の場合あり]value |
std::variant |
type[安全な]union |
std::any |
[任意]タイプ |
std::string_view |
文字列[ビュー]([ゼロコピー]) |
| クラステンプレートのパラメータ[推導] | [記述不要] pair<int, int> [了] |
2. C++20[新機能]
(1) 2.1 [概念](Concepts)
[機能]: [制約]テンプレートパラメータ、[エラー]メッセージをより分かりやすくする。
例:[概念による制約]テンプレート (難易度 ⭐⭐⭐)
#include iostream
#include concepts
// T
template<typename T>
requires std::integralT
T doubleValue(T x) {
return x * 2;
}
int main() {
std::cout << doubleValue(10) << std::endl; // 20
// std::cout << doubleValue(3.14) << std::endl; // ❌ compilation()
return 0;
}
(2) 2.2 [範囲ライブラリ](Ranges)
[機能]: [より簡潔な]STLアルゴリズム[の呼び出し方法]。
例:[用]範囲 (難易度 ⭐⭐)
#include iostream
#include ranges
#include vector
int main() {
std::vectorint v = {1, 2, 3, 4, 5};
// C++20:ranges
auto result = v | std::views::filter((int x) { return x % 2 == 0; })
| std::views::transform((int x) { return x * 10; });
for (int x : result) {
std::cout << x << " "; // output:20 40
}
std::cout << std::endl;
return 0;
}
(3) 2.3 [コルーチン](Coroutines)
[機能]: [コルーチンのサポート]([一時停止]/[再開]機能)。
例:[簡単なコルーチーン]([難易度]⭐⭐⭐⭐)
#include iostream
#include coroutine
// C++20,
//:,
int main() {
// C++20coroutine()
std::cout << "C++20 Coroutines" << std::endl;
return 0;
}
(4) 2.4 [その他]C++20[機能]
| [特性] | [説明] |
|---|---|
| [モジュール](Modules) | ヘッダーファイルを[置き換え]、コンパイルを[高速化] |
| [コルーチン](Coroutines) | [非同期プログラミング] |
| [日付ライブラリ](chrono[拡張]) | [より優れた日付・時刻の処理] |
| [フォーマットライブラリ](format) | Pythonのformatに似たクラス |
| [宇宙船]演算子(<=>) | [三項比較] |
3. [新機能の選択に関する提案]
(1) 3.1 [どの基準を用いるべきか]?
| [標準] | [推奨] |
|---|---|
| C++11 | [最低要件]、[必須] |
| C++14 | [小幅な改善]、[推奨] |
| C++17 | [多くの実用的な機能]、[推奨] |
| C++20 | [最新機能],[注意して使用](コンパイラ[サポートが不完全な場合あり]) |
4. 演習:[C++17]を使ったコードのリファクタリング
▶ サンプル 1: [構造化バインディングによる簡略化]code (難易度 ⭐⭐)
#include iostream
#include map
#include string
int main() {
std::map<int, std::string> students = {{1, "..."}, {2, "..."}};
// C++17:bindmap
for (const auto& [id, name] : students) {
std::cout << id << ": " << name << std::endl;
}
return 0;
}
❓ よくある質問
bash g++ -std=c++17 main.cpp g++ -std=c++20 main.cpp 📖 まとめ
| [規格] | [主な特性] |
|---|---|
| C++17 | [構造化バインディング]、if constexpr、[折りたたみ式] |
| C++20 | [概念]、Ranges、[コルーチンの]、[モジュール] |
📝 練習問題
-
**初心者(難易度 ⭐):[区別] auto、[範囲] for、nullptr を使用して、[古い形式の] C++ コード[の断片]([明示的な]型、[従来の] for、NULL)を書き換えてください。
-
**中級(難易度 ⭐⭐):[構造化バインディング](C++17)を使用して、[pair] または [tuple] の [返り値] を [展開] し、[従来の] .first/.second [方式] と[比較]する。
-
**上級(難易度 ⭐⭐⭐):[用] constexpr(C++17)が[実装]され、[コンパイル]段階の[分岐]が行われる場合、[関数テンプレート]を[作成]し、[整数]型と[浮動小数点]型に対して[異なる処理ロジック]を適用する。
- C++11:auto/nullptr/[範囲]for/lambda/スマートポインタ
- C++14:genericlambda/make_unique
- C++17:[構造化バインディング]/if constexpr/string_view/file[システム]
- C++20:[モジュール]/[コルーチーン]/[概念]/Ranges
- [本コースでは] C++17を採用し、[新機能と互換性のバランス]を図っています
[次のレッスン]:パフォーマンス最適化(#49)



