C++/キーワード

カテゴリ:Book:C++#キーワード%20

キーワード

C++のキーワードは次のとおりです[1]

alignas alignof asm auto bool break case catch char char8_t char16_t char32_t class concept const consteval constexpr constinit const_cast continue co_await co_return co_yield decltype default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new noexcept nullptr operator private protected public register reinterpret_cast requires return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while

C++のキーワード一覧表
キーワード用途
alignasalignas(alignment) でアライメントを指定する
alignas(type) で型のアライメントを指定する
alignofalignof(type) で型のアライメント値を取得する
asmasm(assembly-instruction) でインラインアセンブリを挿入する
autoauto x{value}; で型を自動推論する
auto &x = ref; で参照型を推論する
auto add(int a, int b) -> int { return a + b; } で関数を後置戻値型宣言する
boolbool x{true}; で真理値型を定義する
breakbreak; でスイッチ文やループから抜ける
casecase value: statement; でスイッチ文のケースを記述する
catchtry { /* code */ } catch(Exception e) { /* handler */ } で例外をキャッチする
charchar c{'a'}; で文字型を定義する
char8_tchar8_t c8{u8'a'}; でUTF-8文字型を定義する
char16_tchar16_t c16{u'a'}; でUTF-16文字型を定義する
char32_tchar32_t c32{U'a'}; でUTF-32文字型を定義する
classclass C { /* members */ }; でクラスを定義する
conceptconcept C = /* constraint */ ; でコンセプトを定義する
constconst int x{1}; で値の不変を指定する
int const x{1}; でも同様
const int* p = &a; でpが指す値は変更不可
int* const q = &b; でq自体は変更不可だが、指す値は変更可能
const int* const r = &a; でr自体も指す値も変更不可
class MyClass {
    int value;
public:
    int getValue() const { return value; }
};
でgetValue()は、オブジェクトの状態を変更しないことを指定
const auto n = 10;で不変性のあるオブジェクトを型推論C++11auto
inline const int k_global = 100;で複数の翻訳単位で一意になることを指定C++17inline
constevalconsteval int f() { /* ... */ } で定数評価関数を定義する
constexprconstexpr int square(int x) { return x * x; } でコンパイル時に評価される定数式を定義(この時点では return 式; にだけ対応)C++11
constexpr int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}
でより複雑な定数式を定義C++14,C++17
if constexpr (condition) {
    // statment1
} else {
    // statment2
}
でテンプレートの実体化の条件付き抑制C++17
constexpr int get_value(bool condition) {
    if (condition) {
        return 42;
    } else {
        throw std::runtime_error("Condition is false");
    }
}
で例外を上げる定数式を定義C++20
constinitconstinit int x{f()}; でデータ初期化に定数式評価を強制する
const_castconst_cast<int*>(p) でconst修飾を除去するキャスト
continuecontinue; で現在の反復を打ち切り、次の反復に移る
co_awaitco_await expr; でコルーチンを一時停止する
co_returnco_return expr; でコルーチンから値を返す
co_yieldco_yield expr; でコルーチンから値を生成する
decltypedecltype(expr) x{expr}; で式の型を推論する
defaultdefault: statement; でスイッチ文のデフォルトケースを記述する
deletedelete p; でヒープ領域のオブジェクトを解放する
delete[] arr; で配列を解放する
dodo { /* loop body */ } while (condition); でdo-while文を記述する
doubledouble x{3.14}; で倍精度浮動小数点型を定義する
dynamic_castdynamic_cast<Type*>(p) で安全なダウンキャストを行う
elseif (cond) { /* true case */ } else { /* false case */ } でif文の否定節を記述する
enumenum E { A, B, C }; で列挙型を定義する
enum class E : int { A, B, C }; でスコープ付き列挙型を定義する
explicitexplicit C(args); で暗黙の型変換コンストラクタを禁止する
exportexport module M; でモジュールエクスポートブロックを開始する
externextern int x; で外部リンケージを指定する
falsebool x{false}; で偽の真理値リテラルを使う
floatfloat x{3.14f}; で単精度浮動小数点型を定義する
forfor (init; cond; iter) { /* loop body */ } でfor文を記述する
friendfriend int f(C&); でクラスのフレンド関数を宣言する
friend class D; でフレンドクラスを宣言する
gotogoto label; /* ... */ label: で無条件ジャンプを行う
ifif (cond) { /* true case */ } で条件分岐を行う
if (cond) { /* true */ } else { /* false */ } で条件分岐+否定節
inlineinline f() { /* ... */ } でインライン関数を指定する
inline int x; でインライン変数を指定する
intint x{42}; で整数型を定義する
longlong x{42L}; でlong整数型を定義する
mutablemutable int x; でconstオブジェクト内での変更を許可する
namespacenamespace N { /* ... */ } で名前空間を定義する
namespace { /* ... */ } で無名名前空間を定義する
newint* p{new int}; でヒープ領域にオブジェクトを動的に確保する
int* arr{new int[10]}; で配列を動的に確保する
noexceptnoexcept(true) で関数が例外を送出しないことを指定する
noexcept(false) で関数が例外を送出する可能性があることを指定する
nullptrint* p{nullptr}; でヌルポインタリテラルを使う
operatoroperator+(A, B) { /* ... */ } で演算子をオーバーロードする
privateprivate: /* ... */ でクラスのプライベートメンバーを定義する
protectedprotected: /* ... */ でクラスの保護メンバーを定義する
publicpublic: /* ... */ でクラスのパブリックメンバーを定義する
registerregister int x; で変数をレジスタに配置することを提案する (C++11で非推奨、C++17で廃止)
reinterpret_castreinterpret_cast<int*>(p) でリンタープリットキャストを行う
requirestemplate<typename T> requires C<T> /* ... */ でコンセプト制約を指定する
returnreturn expr; で関数から値を返す
shortshort x{42}; でshort整数型を定義する
signedsigned int x{42}; で符号付き整数型を定義する
sizeofsizeof(type) で型のサイズを取得する
sizeof expr で式の結果の型のサイズを取得する
staticstatic int x; でファイル内での静的ストレージ期間を指定する
static int C::x; でクラスデータメンバーの定義を行う
static int f(); で内部リンケージ関数を宣言する
static_assertstatic_assert(cond, msg); でコンパイル時アサートを行う
static_caststatic_cast<int>(x) で安全な暗黙的な型変換を行う
structstruct S { /* members */ }; で構造体を定義する
switchswitch (expr) { /* cases */ } でスイッチ文を記述する
templatetemplate<typename T> struct X { /* ... */ }; でテンプレートを定義する
thisthis->x = y; でメンバーアクセスの際にオブジェクト自身を参照する
thread_localthread_local int x; でスレッドローカルストレージを指定する
throwthrow ex; で例外を送出する
truebool x{true}; で真の真理値リテラルを使う
trytry { /* code */ } catch (...) { /* handler */ } で例外処理ブロックを記述する
typedeftypedef int Integer; で型エイリアスを定義する (非推奨)
typeidtypeid(x) で実行時型情報を取得する
typenametypename T::x でネストした名前がTypeであることを明示する
unionunion U { /* members */ }; で共用体を定義する
unsignedunsigned int x{42U}; で無符号整数型を定義する
usingusing Integer = int; で型エイリアスを定義する
using namespace N; で名前空間からインポートする
virtualvirtual void f(); で仮想関数を宣言する
class B : virtual public A { /* ... */ }; で仮想基底クラスを指定する
voidvoid f() { /* ... */ } で戻り値のない関数を定義する
void* p{nullptr}; で無型のポインタ型を定義する
volatilevolatile int x; で最適化に反する変数の読み書きを指定する
wchar_twchar_t c{L'a'}; でワイド文字型を定義する
whilewhile (cond) { /* loop body */ } でwhile文を記述する

これに加え、モジュールに関する export import module も構文要素 keyword: の一部を構成する。

演算子の代替表現もキーワードに準じた扱いとなります[1]and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq

カテゴリ:Book:C++#キーワード%20

脚注

  1. 1 2 Working Draft, Standard for Programming Language C++, Document Number: N4950, Date: 2023-05-10 :: §5.11 Keywords P-20
カテゴリ:C++
カテゴリ:Book:C++ カテゴリ:C++ カテゴリ:Pages using the JsonConfig extension