关键字是那些含义已经由Compiler定义的单词。这些关键字不能用作标识符。请注意,关键字是保留字和预定义标识符的集合。预定义标识符是由编译器定义的标识符,但用户可以更改其含义。
例如,您可以在main函数中声明一个名为main的变量,对其进行初始化,然后打印出它的值(但只能这样做来验证您可以!)。另一方面,您不能使用名为else的变量来执行此操作。区别在于else是保留字,而main是“仅”预定义标识符。
C ++中总共有95个保留字。C ++的保留字可以方便地分为几组。在第一组中,我们将那些也存在于C编程语言中并被带入C ++的语言。其中有32个。
还有30个不在C中的保留字,因此对于C ++编程语言来说是新的。
当使用标准ASCII字符集时,有11C ++保留字不是必需的,但已添加了这些保留字,以为一些C ++运算符提供可读的替代方式,并且还有助于使用缺少以下字符所需的字符集进行编程C ++。
这是所有这些保留字的列表:
| alignas (since C++11) | double | reinterpret_cast |
| alignof (since C++11) | dynamic_cast | requires (since C++20) |
| and | else | return |
| and_eq | enum | short |
| asm | explicit | signed |
| atomic_cancel (TM TS) | export(1) | sizeof(1) |
| atomic_commit (TM TS) | extern(1) | static |
| atomic_noexcept (TM TS) | false | static_assert (since C++11) |
| auto(1) | float | static_cast |
| bitand | for | struct(1) |
| bitor | friend | switch |
| bool | goto | synchronized (TM TS) |
| break | if | template |
| case | import (modules TS) | this |
| catch | inline(1) | thread_local (since C++11) |
| char | int | throw |
| char16_t (since C++11) | long | true |
| char32_t (since C++11) | module (modules TS) | try |
| class(1) | mutable(1) | typedef |
| compl | namespace | typeid |
| concept (since C++20) | new | typename |
| const | noexcept (since C++11) | union |
| constexpr (since C++11) | not | unsigned |
| const_cast | not_eq | using(1) |
| continue | nullptr (since C++11) | virtual |
| co_await (coroutines TS) | operator | void |
| co_return (coroutines TS) | or | volatile |
| co_yield (coroutines TS) | or_eq | wchar_t |
| decltype (since C++11) | private | while |
| default(1) | protected | xor |
| delete(1) | public | xor_eq |
| do | register(2) |