C++17
表示
C++17は、プログラミング言語C++の国際規格ISO/IEC 14882:2017の略称である。
廃止された機能
[編集]今回の改定では新機能の追加のみではなく、いくつかの機能が廃止されている。
- トライグラフの廃止[1][2]。
- 標準ライブラリから、すでに非推奨になっていた
std::auto_ptr,std::random_shuffleを含むいくつかの型や関数、古い関数アダプタが取り除かれた[3][4]。これらはC++11でstd::unique_ptr、std::shuffle、std::bind、ラムダのような改良された手段に取って代わられた。 - (すでに非推奨となっていた) 記憶域クラス指定子としてのキーワード
registerの廃止[5]。キーワード自体は未使用で予約される。
新機能
[編集]言語
[編集]static_assertの診断メッセー���を省略可能とした[6]。- テンプレートのテンプレートのパラメータに (
classの代わりに)typenameを使えるようにした[7]。 - 初期化リストからの型推論 (
auto) 規則を新設[8][3]。 - 入れ子の名前空間の定義を
namespace X { namespace Y { … } }等とする代わりにnamespace X::Y { … }と書けるようにした[3][9]。 - 名前空間や列挙型に属性を書けるようにした[10][11]。
- 新たに
[[fallthrough]]、[[maybe_unused]]、[[nodiscard]]属性を標準に追加[12]。 - UTF-8 (
u8) 文字リテラル[10][13][14]。 - 十六進浮動小数点リテラル[15][16]。
- 全ての非型テンプレート引数における定数評価[10][17]。
- 可変長引数テンプレートにおける畳み込み式[10][18]。
if constexpr(expression)の形式によるコンパイル時の静的if[19]。- 構造化束縛宣言。
auto [a, b] = getTwoReturnValues();のように書くことができるようになった[20]。 if文およびswitch文での初期化子[21]。T型 (トップレベルのcv修飾を無視する) の純粋右辺値式からT型のオブジェクトをコピー初期化および直接初期化するとき、純粋右辺値式からコピーコンストラクタやムーブコンストラクタが呼ばれなくなった。詳細は「コピーの省略」を参照のこと。- over-aligned なメモリ確保に関する拡張[22]。
- クラステンプレートの実引数推定 (Class Template Argument Deduction: CTAD)。コンストラクタの推定ガイドが導入されたことで、
std::pair<double, bool>(5.0, false)のようなコンストラクタの引数の型や、std::make_pair(5.0, false)のようなヘルパー関数テンプレートが必要なくなり、std::pair(5.0, false)のように書くことができるようになった[23][24]。 - インライン変数により、単一定義規則に反することなくヘッダファイルで変数を定義できるようになった。効果はインライン関数と同様である。
__has_includeでヘッダが利用可能かをプリプロセッサディレクティブで確認できるようになった[25]。__cplusplusの値は201703Lとなった[26]。- 例外指定が関数の型の一部となった[27]。
ライブラリ
[編集]以下のヘッダが追加された。
<any>— あらゆる型を保持できるstd::anyクラス<filesystem>— ディレクトリやファイルの操作<optional>— 有効または無効な値を保持するstd::optionalクラス<string_view>— 読み取り専用で所有権を持たずに文字シーケンスを参照するstd::basic_string_viewクラス<variant>— 型安全な共用体(union)std::variantクラス
脚注
[編集]- ^ “N3981: Removing trigraphs??! (Richard Smith)” (2014年5月6日). 2015年4月3日閲覧。
- ^ IBM comment on preparing for a Trigraph-adverse future in C++17, IBM paper N4210, 2014-10-10. Authors: Michael Wong, Hubert Tong, Rajan Bhakta, Derek Inglis
- ^ a b c “Updates to my trip report”. 2014年11月28日閲覧。
- ^ “N4190: Removing auto_ptr, random_shuffle(), And Old <functional> Stuff (Stephan T. Lavavej)”. 2014年11月28日閲覧。
- ^ “C++ Keywords: register”. 2018年9月3日閲覧。
- ^ “N3928: Extending static_assert, v2 (Walter E. Brown)”. 2015年4月3日閲覧。
- ^ “N4051: Allow typename in a template template parameter (Richard Smith)”. 2015年4月3日閲覧。
- ^ “N3922: New Rules for auto deduction from braced-init-list (James Dennett)”. 2014年11月28日閲覧。
- ^ “N4230: Nested namespace definition (Robert Kawulak, Andrew Tomazos)”. 2014年11月28日閲覧。
- ^ a b c d “New core language papers adopted for C++17”. 2014年11月15日閲覧。
- ^ “N4266: Attributes for namespaces and enumerators (Richard Smith)”. 2014年11月15日閲覧。
- ^ “N4640: Working Draft, Standard for Programming Language C++”. pp. 193–195. 2017年3月9日閲覧。
- ^ “N4267: Adding u8 character literals (Richard Smith)”. 2014年11月15日閲覧。
- ^ UTF-8 文字列リテラルは C++11 から存在する。C++17 では一貫性確保のために対応する文字リテラルが追加されるが、その型は
char型であり、またUTF-8文字リテラルが扱えるコードポイントの範囲は、C0制御文字とラテン基本文字 (C0 Controls and Basic Latin Unicode block) すなわち ASCII に限定されている。なお、C++標準における従来の文字リテラルは、文字符号化方式がASCIIであることが保証されないため、UTF-8文字リテラルはASCIIを保証するという移植性の観点からも有用である。 - ^ Thomas Köppe. “Hexadecimal floating literals for C++”. 2017年7月16日閲覧。
- ^ “N4659: Working Draft, Standard for Programming Language C++”. 2017年7月16日閲覧。
- ^ “N4268: Allow constant evaluation for all non-type template arguments (Richard Smith)”. 2014年11月15日閲覧。
- ^ “N4295: Folding expressions (Andrew Sutton, Richard Smith)”. 2014年11月15日閲覧。
- ^ “N4659: Working Draft, Standard for Programming Language C++”. 2017年3月24日閲覧。
- ^ “N4659: Working Draft, Standard for Programming Language C++”. 2017年3月24日閲覧。
- ^ “Selection statements with initializer”. 2018年10月9日閲覧。
- ^ “Dynamic memory allocation for over-aligned data”. 2017年3月14日閲覧。
- ^ “Class template argument deduction”. 2019年1月30日閲覧。
- ^ “CppCon 2018: Timur Doumler “Class template argument deduction in C++17””. 2019年1月30日閲覧。
- ^ “N4640: Working Draft, Standard for Programming Language C++”. pp. 431–433. 2017年3月11日閲覧。
- ^ “N4659: Working Draft, Standard for Programming Language C++”. 2017年3月24日閲覧。
- ^ “P0012R1: Make exception specifications be part of the type system, version 5”. 2018年12月17日閲覧。