关键字列表

关键字说明与用法举例
alignas

用于声明:指示类型的对齐要求(只能更严格)。

structclass alignas(表达式类型) 类名 {
    成员列表
};
struct alignas(8) S {
    char m;
};
struct alignas(int) S {
    char m;
};
alignof

用于运算符:查询类型的对齐要求。C++ 要求任何类型必须对齐到内存中 2 的整数次幂个字节数。

alignof(类型)
struct S {
    char c;
    int i;
};
int main() {
    alignof(char); //1
    alignof(S); // 4 maybe
}
and

用于运算符:&& 的替代写法。

左侧操作数 and 右侧操作数
x > 0 and x < 5;
and_eq

用于运算符:&= 的替代写法。

左侧操作数 and_eq 右侧操作数
x and_eq (1 << i);
asm

其它:嵌入汇编语言。对于不同编译环境规则不同。

asm(字符串字面量)
// exit in Linux
asm ("movq $60, %rax\n\t"
     "movq $0,  %rdi\n\t"
     "syscall");
atomic_cancel

(实验性功能)用于事务:声明原子块且可被取消。

atomic_cancel 复合语句
int f() {
    static int i{0};
    atomic_cancel {
        ++i;
        return i;
    }
}
atomic_commit

(实验性功能)用于事务:声明原子块且抛出异常。

atomic_commit 复合语句
int f() {
    static int i{0};
    atomic_commit {
        ++i;
        return i;
    }
}
atomic_noexcept

(实验性功能)用于事务:声明原子块且遇异常正常提交。

atomic_noexcept 复合语句
int f() {
    static int i{0};
    atomic_noexcept {
        ++i;
        return i;
    }
}
auto

1. 用于声明:自动推导类型声明符。对于变量,将通过初始化值推导其类型;对于函数,将通过return语句推导其返回类型。对于模板形参,通过实参推导其类型。

auto&&& 声明符 初始化器;
decltype(auto) 声明符 初始化器;
int a = 42;
int& ra = a;
auto a1 = ra; // int
decltype(auto) a2 = ra; // int&
auto f() {
    return 42;
} // int f();

2. 用于声明:声明结构化绑定。

auto&&& [标识符列表] 元组初始化器;
int a[2] = {1, 2};
auto [x, y] = a; // copy
auto& [x, y] = a; // ref
bitand

用于运算符:& 的替代写法

左侧操作数 bitand 右侧操作数
a bitand b;
bitor

用于运算符:| 的替代写法

左侧操作数 bitor 右侧操作数
a bitor b;
bool

用于声明:布尔类型变量的类型说明符。

bool 变量名 初始化器;
bool flag{false};
break

用于控制成分:break 语句跳出当前 for、while、do-while 或 switch 语句。

break;
for(int i{0}; i < 10; i++) {
    if (i == 5) break;
}
case

用于控制成分:在switch语句中,作为语句标号确定入口。

case 常量: 语句
switch(score) {
    case 100: n++; break;
}
catch

用于异常处理:捕获异常并处理。

try
    复合语句
catch (声明序列...)
    复合语句
try {
    str.substr(10);
} catch (std::exception e) {
    std::cerr << e.what();
}
try {
    f();
} catch (...) {
    // who cares?
}
char

用于声明:字符类型变量的类型说明符。

char 变量名 初始化器;
char result{'G'};
char8_t

用于声明:UTF-8 字符类型变量的类型说明符。

char8_t 变量名 初始化器;
char8_t ch{u8'G'}
char16_t

用于声明:UTF-16 字符类型变量的类型说明符。

char16_t 变量名 初始化器;
char16_t hanzi{u'字'};
char32_t

用于声明:UTF-32 字符类型变量的类型说明符。

char32_t 变量名 初始化器;
char32_t emoji{U'🍌'};
class

1. 用于声明:声明默认为私有成员的类类型。

class 类名 {
    成员列表
};
class Student {
public:
    int no;
    std::string name;
    bool sex;
};

2. 用于声明:声明有作用域枚举类型。

enum classstruct 枚举名 {
    枚举项列表
};
enum class Color {
    Red,
    Blue,
    Green
};

3. 用于模板:用于引入类型模板形参,与 typename 等价。

template <classtypename 类型形参>
声明
template <class T>
T& max(const T& a,const T& b) {
    return a > b ? a : b;
}
compl

用于运算符:~的替代写法。

compl 操作数
compl a;
concept

用于约束:定义一个概念(又称约束集合)。

template <模板形参列表>
concept 概念名 = 约束表达式;
template <typename T, typename U>
concept Derived = std::is_base_of<U, T>::value;
const

1. 用于声明:限定变量只读。

const 类型说明符 变量名 初始化器;
const float PI{3.14f};

2. 用于声明:限定成员函数对成员只读。

返回类型 函数名(形参列表) const;
struct Coord {
    int x;
    int y;
    double mod() const {
        return std::sqrt(x * x + y * y);
    }
};
consteval

1. 用于声明:指示函数为即时函数(Immediate Function),即每次调用必须在产生编译期常量。

consteval 返回类型 函数名(形参列表);
consteval int sqr(int n) {
    return n * n;
}

2. 用于元编程:判断当前语境是否为常量求值。

if !consteval 真分支复合语句
else 假分支复合语句
constexpr int f() {
    if consteval {
        return 1; // f() 在常量表达式中被调用
    } else {
        return 0; // f() 在运行时调用
    }
}
constexpr

1. 用于声明:指示函数或者变量的值可以用于常量表达式。对于变量,限定为只读的。

constexpr 类型说明符 变量名 初始化器;
constexpr 返回类型 函数名(形参列表);
constexpr int fact (int n) {
    return n <= 1 ? 1 : (n * factorial(n - 1));
}
constexpr int a{fact(4)};

2. 用于元编程:表明 if 语句含义为条件编译而非条件运行。

if constexpr (常量表达式) 真分支语句
else 假分支语句
void f() {
    if constexpr (true) {
        i++;
    }
}
constinit

用于声明:指示变量的初始化必须为静态初始化(零初始化或者常量初始化),但不限定为只读的。

constinit 类型说明符 变量名 初始化器;
constexpr int fact (int n) {
    return n <= 1 ? 1 : (n * factorial(n - 1));
}
constinit int a{fact(4)};
const_cast

用于运算符:对于指针和引用进行转换,可以移除常量性。

const_cast<新类型>(指针或引用)
const int a = 42;
int* pa = const_cast<int*>(&a);
int& ra = const_cast<int&>(a);
continue

用于控制成分:continue 语句用于跳过 for、while 或 do-while 的剩余部分。

continue;
for (int i{0}; i < 10; i++) {
    if(i != 5) continue;
}
co_await

用于协程:此运算符暂停当前协程直至等待体恢复。

co_await 等待体
task<> tcp_echo_server() {
    char data[1024];
    for (;;) {
        std::size_t n = co_await socket.async_read_some(buffer(data));
        co_await async_write(socket, buffer(data, n));
    }
}
co_return

用于协程:此语句完成协程执行,并返回一个值。

co_return 返回值;
lazy<int> f() {
    co_return 7;
}
co_yield

用于协程:此运算符暂停当前协程并返回一个值。

co_yield 操作数
generator<int> iota(int n{0}) {
    while(true)
        co_yield n++;
}
decltype

1. 用于声明等:检查实体或表达式的类型。

decltype(实体)
decltype(表达式)
int a;
decltype(a) x; // int
decltype((a)) y; // int&
decltype(42) z; // int

2. 用于声明:配合auto说明符,保留引用语义。

decltype(auto) 变量名 初始化器;
int a;
int& ra{a};
decltype(auto) a2{ra}; // int&
default

1. 用于控制成分:在switch语句中,作为语句标号确定默认入口。

default: 语句
switch (score) {
    default: n++;
}

2. 用于声明:显示声明预置成员函数(默认构造函数、复制构造函数、移动构造函数、赋值运算符、析构函数和三路比较运算符)。

返回类型 函数名(形参列表) = default;
struct S {
    int m;
    S() = default;
    auto operator<=>(const S&) const = default;
};
delete

1. 用于运算符:释放 new 表达式分配的指针所指向的内存。

delete 指针
delete[] 指针
int* p1 = new int(42);
int* p2 = new int[10];
delete p1;
delete[] p2;

2. 用于声明:声明成员函数被弃置

返回类型 函数名(形参列表) = delete;
struct S {
    S(const S&) = delete;
};
do

用于控制成分:形成do-while循环。

do
    语句
while (表达式);
int i{0};
do {
    i++;
} while (i < 10);
double

用于声明:双精度浮点类型变量的类型说明符。

double 变量名 初始化器;
double score{60.5};
dynamic_cast

用于运算符:将指向类的指针(引用)沿继承层级安全地转换到其它类的指针(引用)。若转型失败,返回空指针。(若为引用,抛出异常。)

dynamic_cast<新类型>(指针或引用)
struct Base {
    virtual ~Base() {}
}
struct Derived : Base {}
int main(){
    Base* b{new Base};
    Derived* d{dynamic_cast<Derived*>(b)};
else

用于控制成分。形成 if 语句的假分支。

if (条件)
    真分支语句
else
    假分支语句
if (flag) {
    std::cout << "Yes";
else
    std::cout << "No";

   
enum

用于声明:声明枚举类型。

enum classstruct 枚举名 : {
    枚举项列表
};
enum Color { Red, Black };
enum class Status { Free, Busy };
explicit

用于声明:限制构造函数为显式,即不能用于隐式转换和复制初始化。可后接常量表达式,当常量表达式为true时为显式。

explicit 构造函数名(形参列表);
explicit(常量表达式) 构造函数名(形参列表);
struct A {
    explicit A(int) { }
};
struct B {
    explicit (true) B(int) { }
};
int main() {
    A a(42); // not A a = 42;
}
export

用于模块:标记一个声明或一个模块被当前模块导出。

export module 模块名;
export 声明;
// hello.cpp
export module my.app.hello;
export auto sayHello() {
    return "Hello!";
}
// main.cpp
import my.app.hello;
import <iostream>
int main() {
    std::cout << sayHello();
}
extern

1. 用于声明:声明对象具有静态存储期和外部连接;即在其它文件中寻找对象的定义。

extern 类型说明符 变量名;
// score.cpp
int score{60};
// main.cpp
#include <iostream>
extern int score;
int main() {
    std::cout << score;
} // 60

2. 其它:用于连接其它语言编写的代码。一般只支持C语言。

extern 字符串字面量 { 声明序列 }声明
extern "C" {
    static void f();
}
extern "C" void g() {
     printf("Hello");
}

3. 用于模板:显式实例化声明类模板,但跳过隐式实例化步骤;即在其它文件中寻找实例化定义。

extern template classstruct 模板名模板实参列表;
template<typename T> struct Z { };
// find instantiation in other place
extern template struct Z<double>;
false

用于数据成分:表示假的布尔类型字面值。

false
bool flag{false};
float

用于声明:单精度浮点类型变量的类型说明符。

float 变量名 初始化器;
float score{60.5f};
for

1. 用于控制成分:形成for循环语句。

for (初始语句 条件表达式; 迭代表达式)
    语句
for (int i{0}; i < 10; i++){
    std::cout << i;
}

2. 用于控制成分:形成基于范围的for循环语句。范围可以是数组或容器等。

for (范围声明 : 范围表达式)
    语句
int a[10]{};
for (auto i : a) {
    i++;
}
friend

用于声明:声明函数或类为友元函数或友元类。友元函数和友元类可以访问其它类中的私有成员。

friend 函数声明
friend 类名;
struct Y;
class X {
    friend Y;
    friend void g();
    int a = 0;  // private
};
X x;
struct Y {
    int b = x.a;
};
void g() {
    x.a;
}
goto

用于控制成分:goto语句可无条件跳转到对应标号的语句。

goto 标号;
loop: x++;
if (x < 10) goto loop;
if

用于控制成分:if语句形成简单的分支流程。

if constexpr (初始化语句 条件表达式)
    真分支语句
else
    假分支语句
if (flag) {
    std::cout << "Yes";
}
if (int x = getRes(); flag)
    std::cout << x;
else std::cout << -x;
inline

1. 用于声明:声明函数或变量为内联的。内联的声明可以容许多次等同的定义;对于内联函数可能会优先内联而不跳转(这是已经被标准抛弃的说法,但仍然适用)。

inline 函数或对象声明
inline max(const int& a, const int& b) {
    return a > b ? a : b;
}

2. 用于命名空间:声明命名空间为内联的,即该命名空间的成员如同视为外层命名空间的成员。

inline namespace 命名空间名 { 声明序列 }
namespace literals {
    inline namespace string_literals {//[...]
    }
}
int

用于声明:基础有符号整数类型变量的类型说明符,或者作为其它整型变量的类型说明符的部分。

int 变量名 初始化器;
int number{42};
long

用于声明:有符号长整型变量的类型说明符,或者作为其它整型变量的类型说明符的部分。

long 变量名 初始化器;
long number{42L};
mutable

1. 用于声明:容许包含该对象的对象被声明为只读时仍可修改。

mutable 类型说明符 变量名 初始化器;
class X {
    mutable int m;
    void f() const {m++;
    }
};

2. 用于 Lambda 表达式:允许 Lambda 表达式函数体修改复制捕获的实参。

[捕获](参数列表) mutable -> 返回类型 函数体
int a = 3;
auto add = [a]() mutable -> void { a++; };
namespace

1. 用于命名空间:建立命名空间或引入命名空间,从而避免命名冲突。

inline namespace 命名空间名 { 声明序列 }
using namespace 命名空间名;
using namespace std;
namespace MyApp {
    int x;
}
int main() {
    MyApp::x;
}

2. 用于命名空间:创立命名空间别名。

namespace 别名 = 命名空间名;
namespace foo {
    namespace bar {int m;
    }
}
namespace fb = foo::bar;
int main() {
    fb::m;
}
new

用于运算符:创建并初始化具有动态存储期的对象,返回指向该对象的指针或者指向数组首元素的指针。

new 类型 初始化器;
new 类型长度初始化器;
int* p1 = new int(42);
int* p2 = new int[10];
delete p1;
delete[] p2;
noexcept

1. 用于声明:指示该函数不会抛出异常,从而编译时优化。可后随常量表达式,当表达式为 true 时不会抛出异常。

返回类型 函数名(形参列表) noexcept;
返回类型 函数名(形参列表) noexcept(常量表达式);
void f() noexcept {}
void g() noexcept(false) {} // g may throw exception
void h() noexcept(true) {}

2. 用于运算符:检查表达式是否可能抛出异常。若不可能,返回 true ;否则返回 false。该运算符不对表达式求值。

noexcept(表达式)
void f() noexcept {}
void g() {}
int main() {
    noexcept(f()); // true
    noexcept(g()); // false
}
not

用于运算符:!的替代写法。

not 操作数
not isOK;
not_eq

用于运算符:!=的替代写法。

左侧操作数 not_eq 右侧操作数
a not_eq b;
nullptr

用于数据成分:空指针字面量,可与其余类型指针隐式或显式转换。

nullptr
int* pointer = nullptr;
operator

用于声明:声明重载运算符。

返回类型 operator运算符 (形参列表);
struct A {
    int m;
    bool operator<(const A& b) const {return m < b.m;
    }
};
or

用于运算符:||的替代写法。

左侧操作数 or 右侧操作数
a or b;
or_eq

用于运算符:|=的替代写法。

左侧操作数 or_eq 右侧操作数
a or_eq b;
private

用于面向对象:指明其后的成员为私有成员,或者指明该类为私有继承。

private:
classstruct 类名 : private 父类名 {
    成员列表
};
struct A {
private:
    int m;
};
struct B : private A {};
protected

用于面向对象:指明其后的成员为受保护成员,或者指明该类为受保护继承。

protected:
classstruct 类名 : protected 父类名 {
    成员列表
};
struct A {
protected:
    int m;
};
struct B : protected A {};
public

用于面向对象:指明其后的成员为公有成员,或者指明该类为受公开继承。

public:
classstruct 类名 : public 父类名 {
    成员列表
};
class A {
public:
    int m;
};
class B : public A {};
reflexpr

(实验性功能)用于反射。获取某个类型的反射元类型。(元类型是指包含这个类的元数据的类型,如类型名、成员列表元组等。)

reflexpr(reflexpr操作数)
struct S { /* ... */ };
using meta_S = reflexpr(S);
register

(此关键字被保留,暂时不使用。)

reinterpret_cast

用于运算符:将指针的基类型重解释为新的基类型。

reinterpret_cast<新类型>(指针)
int a = 42;
int* pa = &a;
long* pb = reinterpret_cast<long*>(pa);
requires

1. 用于概念:requires 子句对模板实参或函数声明指定一个约束,这个约束可以是概念(约束集合)或者它们的合取、析取或原子约束,或者一个 requires 表达式。

template<模板形参列表> requires requires子句
声明
template<模板形参列表>
函数声明 requires requires子句
template<typename T> requires Addable<T>
T f(T a, T b) {
    return a + b;
}
template<typename T>
T g(T a, T b) requires Addable<T> {
    return a + b;
}

2. 用于概念:requires表达式构造一个约束。

requires (形参列表) {
    要求序列
}
template<typename T>
concept Addable = requires (T x) { x + x; }
return

用于函数:终止当前函数。若可能,则返回值给上级函数。

return 表达式;
void f(int i) {
    if (i == 2) return;
    std::cout << i;
}
int square(int x) {
    return x * x;
}
short

用于声明:有符号短整型变量的类型说明符,或者作为其它整型变量的类型说明符的部分。

short 变量名 初始化器;
short i;
signed

用于声明:基础有符号整数类型的类型说明符,或者作为其他有符号整型变量的类型说明符的部分。

signed 变量名 初始化器;
signed score{60}; // same as int
sizeof

1. 用于运算符:sizeof 运算符查询操作数或类型的大小。它是不求值运算符。

sizeof(类型)
sizeof 操作数
sizeof(int);
sizeof 42;

2. 用于运算符:sizeof... 运算符查询形参包所含的元素数量。

sizeof...(形参包)
template <typename... Args>
std::size_t f() {
    return sizeof...(Args);
}
static

1. 用于声明:声明全局变量具有静态存储期和内部连接,即仅能在本文件中使用。

static 类型说明符 变量名 初始化器;
static int num{42};

2. 用于声明:声明局部变量具有静态存储期,即内存分配时段与程序本身相同,仅初始化一次。

static 类型说明符 变量名 初始化器;
void f() {
    static int v{42};
    v++;
    std::cout << v;
}
int main() {
    f(); // 43
    f(); // 44
}

3. 用于声明:声明静态成员,即该成员不绑定到类的实例,为所有该类对象共有。

static 成员或函数声明
class X {
    static int n{42};
}
int main() {
    X::n;
}
static_assert

其它:进行编译期断言检查。若常量表达式为真则不执行操作,否则编译错误并提示字符串字面量的内容(若有)。

static_assert(常量表达式, 字符串字面量);
static_assert(false, "Message");
// Compilation Error: Message
static_cast

用于运算符:进行显式类型转换,但不能移除指针或引用的 const 或 volatile 限定。任意隐式转换均可用词运算符显式转换。亦存在到 void 或 void 的指针的转换、整数类型和枚举类型的转换。

static_cast<类型>(操作数)
int n = static_cast<int>(3.14);
struct

1. 用于声明:声明默认为公有成员的类类型,即结构体类型。

struct 类名 {
    成员列表
};
struct Student {
    int no;
    std::string name;
    bool sex;
};

2. 用于声明:声明有作用域枚举类型。

enum classstruct 枚举名 {
    枚举项列表
};
enum struct Color {
    Red,
    Blue,
    Green
};
switch

用于控制成分:switch语句形成多分支结构。

switch (初始语句 表达式) {
    带case或default标号的语句
}
switch (score) {
    case 0: a++; break;
    case 1: b++; break;
    default: c++;
}
synchronized

(实验性功能)用于事务:指明复合语句为同步块,即如同在全局锁下执行。

synchronized 复合语句
int f() {
    static int i = 0;
    sychronized {++i;return i;
    }
}
template

用于模板:声明模板。可以是类模板、函数模板、别名模板、变量模板或者概念。

template<形参列表> requires子句
    类、变量或函数声明
template<形参列表>
using 别名 = 类型标识;
template<形参列表>
concept 概念名 = 约束表达式;
template<typename T>
T& max(const T& a, const T& b) {
    return a > b ? a : b;
}
template<typename T>
using Ptr = T*;
template<typename T>
concept Derived = std::is_base_of<U, T>::value;
this

1. 用于面向对象:非静态成员函数中可使用的一个指针,指向当前正在调用成员函数的对象。对于允许 this 关键字的环境,调用非静态成员时会自动添加隐含的 this->

this
struct S {
    int x;
    void add() {this->x += 1;
    }
};

2. 用于面向对象:显式给出隐含的 *this 参数。

返回值类型 成员函数名(this 类型说明符 参数名, 其它参数);
struct A {
  int v;
  void setV(this const A& self, int v) {
    self.v = v;
  }
  template <typename Self>
  void f(this Self&& self, int v) {
    // perfect-forwarding *this in member function
    otherFunc(std::forward<Self>(self));
  }
}
thread_local

用于声明:声明对象具有线程存储期。对象的存储在线程开始时分配,而在线程结束时解分配。

thread_local 类型说明符 变量名 初始化器;
thread_local unsigned int rage{1};
throw

用于运算符:将表达式复制并作为异常抛出,或者重新抛出当前正在处理的异常。

throw 表达式
try {
    throw std::overflow_error("too big");
} catch (const std::exception& e) {
    throw; // overflow_error
}
true

用于数据成分:表示真的布尔类型字面值。

true
bool flag = true;
try

用于异常处理:关联一个复合语句使得其中的异常可以被处理。

try
    复合语句
catch (声明序列...)
    复合语句
try {
    str.substr(10);
} catch (std::exception e) {
    std::cerr << e.what();
}
typedef

用于声明:创建替代类型名的别名,但不能是模板名。此声明将声明符的名字作为类型的别名。

typedef 类型名 声明符;
typedef unsigned int uint;
typedef int arr10OfInt[10];
typedef void (*ptr2Func)();
typeid

用于运算符:查询类型的信息,或者操作数的类型的信息,返回std::type_info对象,可以通过name()成员函数获取类型名。对于部分编译环境,此结果可能需要用 c++filt 进行转译。

typeid(类型)
typeid(表达式)
#include <typeinfo> // must
struct Base {
    virtual ~Base() {}
};
struct Derived : Base {};
int main() {
    Base* ptr = new Derived;
    typeid(*ptr).name(); // Derived or 7Derived or sth else
    typeid(int* (*)()).name();// int* (*)* or PFPivE or sth else
}
typename

1. 用于模板:在模板声明中用以引入类型模板形参,与class等价。

template <classtypename 类型形参>
声明
template <typename T>
T& max(const T& a,const T& b) {
    return a > b ? a : b;
}

2. 用于模板:在模板声明中指明某个待决名为类型,以区别于静态成员变量,防止编译错误。

typename 待决名余下声明;
template<typename T>
void f(T t) {
    typename T::iterator iter;
    iter = t.begin();
}

3. 用于概念:在 requires 表达式的要求中,用于校验类型名是否合法。

typename 类型名
template<typename T>
concept HaveInner =
requires {
    typename T::inner;
};
union

用于声明:声明联合体,即一个时刻只能保有一个非静态数据成员的类型。

union 联合体名 { 成员列表 };
union ID {
    int no;
    char name[32];
};
unsigned

用于声明:基础无符号整数类型的类型说明符,或者作为其他无符号整型变量的类型说明符的部分。

unsigned 变量名 初始化器;
unsigned score = 60;
using

1. 用于命名空间:引入命名空间或者命名空间中的对象。

using namespace 命名空间名;
using 标识符列表;
using namespace std;
using std::cin, std::cout;

2. 用于声明:用于创建指代类型名的别名,与typedef类似。亦可以创建别名模板。

using 别名 = 类型标识;
template<形参列表>
using 别名 = 类型标识;
using uint = usigned int;
using array10OfInt = int[10];
using ptr2Func = void (*)();
template<typename T>
using Ptr = T*;
virtual

1. 用于面向对象:声明成员函数为虚函数,即在多态场景下,其可以被子类成员同名函数所覆盖。亦可声明为纯虚函数,则该类成为抽象类且不得有实例。

virtual 返回类型 函数名(形参列表)[ = 0];
struct Base {
    virtual void f() {std::cout << "Base Func
  Called";
    }
};
struct Derived : public Base {
    void f() {std::cout << "Derived
  Called";
    }
};
int main() {
    Base* b = new Derived;
    b->f(); // Derived Called
}

2. 用于面向对象:声明继承方式为虚继承,即在多重继承中,最终派生对象只有一个基类成员。

classstruct 类名 : virtual privateprotectedpublic 父类名 {
    成员列表
};
struct A {
    int n;
};
struct B : virtual public A {};
struct C : virtual public A {};
struct D : B,C {
    void f() {
        n;
        B::n;
        C::n; // same
    }
};
void

1. 用于声明:空类型的类型说明符。空类型是值的集合为空集的类型,不存在空类型的对象,但存在指向空类型的指针和返回空类型的函数。

void* 指针名;
void 函数名(形参列表);
void f() {   
    int a{42};
    void* pa{reinterpret_cast<void*>(&a)};
}
volatile

1. 用于声明:声明该变量只为易变的,即它的值可能会以某些不可检测的方式发生改变。因此此类变量每次修改将读写内存而非寄存器,且避免编译器进行优化。

volatile 类型说明符 变量名 初始化器;
volatile double result{2.56};

2. 用于声明:声明成员函数为易变的,即它所访问的成员的值可能会以某些不可检测的方式发生改变。

返回类型 函数名(形参列表) volatile;
struct S {
    void f() volatile {}
};
wchar_t

用于声明:宽字符类型变量的类型说明符。宽字符类型是长度足够表示当前环境任何可编码字符的类型。在Linux中,一般为32位长度,但在Windows中为16位长度。

wchar_t 变量名 初始化器;
wchar_t hanzi = L'字';
while

用于控制成分:形成while循环。

while (条件)
    语句
int i = 0;
while (i < 10) {
    i++;
}
xor

用于运算符:^的替代写法。

左侧操作数 xor 右侧操作数
a xor b;
xor_eq

用于运算符:^=的替代写法。

左侧操作数 xor_eq 右侧操作数
flag xor_eq true;

以下名字是有特殊含义的标识符:它们和关键字一样在特定场合有特殊含义,但可以用做变量名(尽管并不建议)。

标识符说明与用法举例
override

用于面向对象:指定某子类函数必将覆盖基类中的虚函数。

返回类型 成员函数名(形参列表) override;
struct A {
    virtual void f() = 0;
};
struct B : A {
    void f() override {}
};
final

用于面向对象:指定该类不可继承。

classstruct 类名 final {
    成员列表
};
struct A final {};
// error: struct B : A ;
import

用于模块:导入一个模块。也兼容将一个头文件作为模块导入。

import 模块名;
import <头文件>;
// main.cpp
import my.app.hello;
import <iostream>
int main() {
    std::cout << sayHello();
}
module

用于模块:声明一个模块实现单元(Module Implementation Unit)或者模块接口单元(Module Interface Unit)。

export module 模块名;
// hello.cpp
export module my.app.hello
export auto sayHello();
// hello_impl.cpp
module my.app.hello
auto sayHello() {
    return "hello!";
}

用于模块:开始全局模块片段。

module : private;
module;
#include <cassert>
export module A;
// [...]
transaction_safe

(实验性功能)用于事务:声明某函数为事务安全的。

返回类型 函数名(形参列表) transaction_safe;
int f() transaction_safe {}
transaction_safe_dynamic

(实验性功能)用于事务:声明某虚函数为事务安全的,即它的最终覆盖函数也是事务安全的。

virtual 返回类型 函数名(形参列表) transaction_safe_dynamic;
struct S {
    virtual int f() transaction_safe_dynamic {}
};
最近更新: 12/18/2023, 5:11:06 AM
代码未运行