C++ Glimpse
关键字, 最基础的用法, 以及一些常用的参考网站, 以期可以能够对繁杂的内容确定边界, 日后可以有目标地不断填充
# 关键字是最好的边界:C++ Keywords (opens new window)
一些常见的C++关键字
一些不那么常见的关键字:
Keywords | Description | Keywords | Description |
---|---|---|---|
alignas (opens new window) (since C++11) | alignof (opens new window) (since C++11) | ||
and_eq (opens new window) | asm (opens new window) | ||
atomic_cancel (opens new window) (TM TS) | atomic_commit (opens new window) (TM TS) | ||
atomic_noexcept (opens new window) (TM TS) | bitand (opens new window) | ||
bitor (opens new window) | compl (opens new window) | ||
concept (opens new window) (since C++20) | consteval (opens new window) (since C++20) | ||
constinit (opens new window) (since C++20) | co_await (opens new window) (since C++20) | ||
co_return (opens new window) (since C++20) | co_yield (opens new window) (since C++20) |
# C++ Preprocess(预处理)
一些预处理指令, 由C++ 预处理器(preprocessor (opens new window))进行处理;
following tokens are recognized by the preprocessor (opens new window) outside the context of a preprocessor directive:
_Pragma
(opens new window) (since C++11)
# conditionally :#if (opens new window) #else (opens new window) #elif (opens new window) #ifdef (opens new window) #ifndef (opens new window) #endif (opens new window) #elifdef, #elifndef (since C++23)
# replace:
- #define (opens new window) #undef (opens new window)
- # : 在运行形参替换的基础上以引号包围,实际上创建一个字符串字面量。
- ##: 在运行形参替换的基础上将结果进行拼接。
- 其他的一些Macro:
- _FILE_ 展开成当前文件名,作为字符串字面量,可用 #line 指令更改(宏常量)
- __LINE: 展开成源文件行号,整数常量,可用 #line 指令更改(宏常量)
- __DATE: 展开成翻译日期,形式为 "Mmm dd yyyy" 的字符串。如果月中日期数小于 10 则 "dd" 的首字符为空格。月份名如同以 std::asctime() 生成(宏常量)
- _TIME_: 展开成翻译时间,形式为 "hh:mm:ss" 的字符串字面量(宏常量)
# Include
- #include (opens new window)
- __has_include (opens new window) (C++17 起) 提供的可以判断是否include这个h文件
- __has_cpp_attribute (opens new window) (C++20 起)
# error
# Implementation defined : behavior control
- #pragma (opens new window)
- _Pragma (opens new window)(C++11 起)
- #pragma STDC
- #pragma once: 编译器只对其分析一次,即使它在同一源文件中(直接或间接)被包含了多次也是如此。
类似的标准方式:
#ifndef LIBRARY_FILENAME_H
#define LIBRARY_FILENAME_H
// 头文件的内容
#endif /* LIBRARY_FILENAME_H */
#pragma pack: 控制后续定义的类和union的最大对齐方式;
2
3
4
5
# File name and line information
# 其他一些预处理指令: defined (opens new window) export (opens new window) (C++20) import (opens new window) (C++20) module (opens new window) (C++20)
# C++ Expression(表达式)
# Operators 运算符
Common operators
assignment (opens new window) increment (opens new window) decrement (opens new window) arithmetic (opens new window) logical (opens new window) comparison (opens new window) member (opens new window) access (opens new window) a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b++a
--a
a++
a--+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b!a
a && b
a || ba == b
a != b
a < b
a > b
a <= b
a >= b
a <=> ba[b]
*a
&a
a->b
a.b
a->*b
a.*bSpecial operators static_cast (opens new window) converts one type to another related type
dynamic_cast (opens new window) converts within inheritance hierarchies
const_cast (opens new window) adds or removes cv (opens new window) qualifiers
reinterpret_cast (opens new window) converts type to unrelated type
C-style cast (opens new window) converts one type to another by a mix of static_cast, const_cast, and reinterpret_cast
new (opens new window) creates objects with dynamic storage duration
delete (opens new window) destructs objects previously created by the new expression and releases obtained memory area sizeof (opens new window) queries the size of a type
sizeof... (opens new window) queries the size of a parameter pack (opens new window) (since C++11)
typeid (opens new window) queries the type information of a type
noexcept (opens new window) checks if an expression can throw an exception (since C++11)
alignof (opens new window) queries alignment requirements of a type (since C++11)
一些可以用别名代替的运算符:
Primary | Alternative | 含义 |
---|---|---|
&& | and (opens new window) | 逻辑与 |
|| | or (opens new window) | 逻辑或 |
! | not (opens new window) | 逻辑非 |
& | bitand (opens new window) | 按位与 |
| | bitor (opens new window) | 按位或 |
&= | and_eq (opens new window) | 按位与后赋值 |
|= | or_eq (opens new window) | 按位或后赋值 |
!= | not_eq (opens new window) | 按位取反后赋值 |
^ | xor (opens new window) | 按位异或 |
^= | xor_eq (opens new window) | 按位异或后赋值 |
~ | compl (opens new window) | 按位取反(Compliment) |
Operator overloading : operator (opens new window)
- Conversions
- Memory allocation
- Other
- 常量表达式: constexpr (opens new window) (C++11 起)
- sizeof (opens new window)(1)
- alignof (opens new window)(alignas)
- typeid (opens new window)
- throw (opens new window)
# Primary expressions
字面量
char (opens new window) wchar_t (opens new window) char16_t (opens new window) (C++11 起) char32_t (opens new window) (C++11 起) char8_t (opens new window) (C++20 起) float (opens new window) double (opens new window) long (opens new window) const char[] const wchar_t[] Bool (true or false) nullptr Std::size_t lambda 表达式 (opens new window) (C++11 起) 折叠表达式 (opens new window) (C++17 起) requires 表达式 (opens new window) (C++20 起)
# Unevaluated expressions: typeid、sizeof、noexcept 和 decltype (C++11 起)
# Discarded-value expressions : void, volatile
# C++ Statement (语句)
标签:
- Goto
- Switch语句中的case标签
- switch语句中default标签
表达式语句(expression statement);
复合语句(compound statement);
选择语句(selection statement);
- If.. Else: if (opens new window)
- switch (opens new window) ... case ...
- If consteval
循环语句(iteration statement);
跳转语句(jump statement);
- Break
- Continue
- Return …
- Return
- goto
- continue (opens new window) – break (opens new window) – goto (opens new window) – return (opens new window)
声明语句(declaration statement);
try 块: Try … catch …
atomic 与 synchronized 块(TM TS)。 atomic 与 synchronized 块用来实现事务性内存 (opens new window)。 synchronized and atomic (opens new window) (TM TS)
synchronized 复合语句 | (1) | (TM TS) |
---|---|---|
atomic_noexcept 复合语句 | (2) | (TM TS) |
atomic_cancel 复合语句 | (3) | (TM TS) |
atomic_commit 复合语句 | (4) | (TM TS) |
(1) synchronized 块 (opens new window),与所有 synchronized 块在一个全序中执行; (2) 在发生异常时中止的 atomic 块 (opens new window); (3) 在发生异常时回滚的 atomic 块 (opens new window);
# C++ 11 新增关键字
- lignas (opens new window) (C++11 起)
- alignof (opens new window) (C++11 起)
- char16_t (opens new window) (C++11 起)
- char32_t (opens new window) (C++11 起)
- constexpr (opens new window) (C++11 起)
- decltype (opens new window) (C++11 起)
- noexcept (opens new window) (C++11 起)
- nullptr (opens new window) (C++11 起)
- static_assert (opens new window) (C++11 起)
- thread_local (opens new window) (C++11 起)
# C++ 17 新增关键字
# C++ 20 新增关键字
- char8_t (opens new window) (C++20 起)
- concept (opens new window) (C++20 起)
- consteval (opens new window) (C++20 起)
- constinit (opens new window) (C++20 起)
- co_await (opens new window) (C++20 起)
- co_return (opens new window) (C++20 起)
- co_yield (opens new window) (C++20 起)
- requires (opens new window) (C++20 起)
# TM_TS (Transactional Memory Technical Specification)
- atomic_cancel (opens new window) (TM TS)
- atomic_commit (opens new window) (TM TS)
- atomic_noexcept (opens new window) (TM TS)
- reflexpr (opens new window) (reflection TS)
- synchronized (opens new window) (TM TS)
# 特殊含义的标识符
它们可用作对象或函数的名字,但在某些语境拥有特殊含义: (module 与 import 指令是预处理指令) override (opens new window) (C++11) final (opens new window) (C++11) import (opens new window) (C++20) module (opens new window) (C++20) transaction_safe (opens new window) (TM TS) transaction_safe_dynamic (opens new window) (TM TS)
# C++ Websites for reference
C++ 标准基金会的官方网站: https://isocpp.org/
http://www.cplusplus.com/
https://en.cppreference.com/ 或者中文版 https://zh.cppreference.com/, 一些专用名词可以方便对照;
C++ 标准委员会的文档列表: https://open-std.org/JTC1/SC22/WG21/docs/papers/
一些常用的额标准, 包括POSIX, C, C++: https://open-std.org/