このページはC++23に採用された言語機能の変更を解説しています。
のちのC++規格でさらに変更される場合があるため関連項目を参照してください。
概要
if文、switch文、範囲for文で初期化文を指定できるが、C++20までその初期化文ではtypedefによる型の別名定義はできたが、usingによる型の別名定義はできなかった。
C++23ではusingによる別名定義もまた初期化文でできるようになる。
仕様
初期化文 (init-statement) に、別名宣言 (alias-declaration) を追加 (typedefはsimple-declarationに含まれる)。
init-statement:
expression-statement
simple-declaration
alias-declaration
例
#include <iostream>
int f() { return 1; }
int main()
{
// C++20 : OK
if (typedef int T; T x = f()) {
std::cout << x << std::endl;
}
// C++20 : NG
// C++23 : OK
if (using T = int; T x = f()) {
std::cout << x << std::endl;
}
}