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