需要记忆的(面试六脉神剑)
coroutine(协程)
特性测试宏
module(模块)
barrier/latch
semaphore
jthread
核心语言特性
- 特性测试宏
__has_cpp_attribute( attribute-token )
该标准定义了一组预处理器宏,对应于c++ 11或更高版本中引入的语言特性或库特性。
- <=>
- 指定初始化
对于非联合聚合,当初始化式子句的数量小于成员数量时,未提供指定初始化式的元素将像上面描述的那样初始化(提供默认成员初始化式,否则为空列表初始化)。
struct A
{
string str;
int n = 42;
int m = -1;
};
A{.m = 21} // Initializes str with {}, which calls the default constructor
// then initializes n with = 42
// then initializes m with = 21
- 范围for循环支持初始化语句
for (T thing = foo(); auto& x : thing.items()) { /* ... */ } // OK
- char8_t
UTF-8字符表示类型
- 新增属性
no_unique_address:允许此数据成员与其类的其他非静态数据成员或基类子对象重叠。
likely/unlikely:允许编译器针对以下情况进行优化:包含该语句的执行路径比任何不包含该语句的执行路径更可能或更不可能
- 聚合初始化支持{}
T object = { .des1 = arg1 , .des2 { arg2 } ... };
T object {.des1 = arg1 , .des2 { arg2 } ... };
- coroutines
协程是一种可以挂起执行以便稍后恢复的函数。协程是无堆栈的:它们通过返回调用者来暂停执行,并且恢复执行所需的数据与堆栈分开存储。这允许异步执行的顺序代码(例如,在没有显式回调的情况下处理非阻塞I/O),还支持惰性计算无限序列和其他用途的算法。
- modules(模块)
// helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() // export declaration
{
std::cout << "Hello world!\n";
}
// main.cpp
import helloworld; // import declaration
int main()
{
hello();
}
- 约束和概念
类模板、函数模板和非模板函数(通常是类模板的成员)可以与约束相关联,约束指定了对模板参数的要求,可用于选择最合适的函数重载和模板特殊化。
这种需求的命名集称为概念。每个概念都是一个谓词,在编译时计算,并成为模板接口的一部分,在那里它被用作约束。
#include <string>
#include <cstddef>
#include <concepts>
// Declaration of the concept "Hashable", which is satisfied by any type 'T'
// such that for values 'a' of type 'T', the expression std::hash<T>{}(a)
// compiles and its result is convertible to std::size_t
template<typename T>
concept Hashable = requires(T a)
{
{ std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
};
struct meow {};
// Constrained C++20 function template:
template<Hashable T>
void f(T) {}
//
// Alternative ways to apply the same constraint:
// template<typename T>
// requires Hashable<T>
// void f(T) {}
//
// template<typename T>
// void f(T) requires Hashable<T> {}
//
// void f(Hashable auto /*parameterName*/) {}
int main()
{
using std::operator""s;
f("abc"s); // OK, std::string satisfies Hashable
// f(meow{}); // Error: meow does not satisfy Hashable
}
标准库特性
- bit
基于bit的操作,包括bit_cast(转换类型),bit_ceil(获得不小于x的2的最小积分次幂),rotl(计算位按左旋转的结果),countl_zero(从最高位开始计算连续0位的个数),popcount(统计一个无符号整型的1位的个数),等等。
- format
支持格式化字符串
- numbers
提供许多数字常量,例如:numbers::pi和numbers::sqrt2
- source_location
表示源代码信息的类,如文件名、行号和函数名
- span
类模板span描述了一个对象,该对象可以引用一个连续的对象序列,该序列的第一个元素位于位置0。span可以具有静态范围(在这种情况下,序列中的元素数量在编译时已知并编码在类型中),也可以具有动态范围。
- syncstream
包括同步输出设备包装器(basic_syncbuf),同步输出流包装器(basic_osyncstream)等
- barrier
可重复使用的线程栅栏。
- latch
一次性的线程栅栏
- semaphore
信号量
- stop_token
查询std::jthread取消请求的接口。stop_token类为其关联的std::stop_source对象提供了检查停止请求是否已经发出或可以发出的方法。它本质上是相关停止状态的线程安全“视图”。
- 日历和时区
属于std::chrono
- make_shared支持数组
- jthread
jthread类表示一个执行线程。它具有与std::thread相同的一般行为,除了jthread在销毁时自动重新连接,并且在某些情况下可以取消/停止。
- starts_with/ends_with
检查是否以某个字符串开头/结尾。
- assume_aligned
通知实现ptr所指向的对象至少与N对齐。
- bind_front
生成一个转发调用包装器,调用这个包装器相当于用它的第一个sizeof…(Args)参数绑定到Args来调用f。
std::bind_front(f, bound_args...)(call_args...)等价于std::invoke(f, bound_args..., call_args...)
- c8rtomb/mbrtoc8
将单个码位从UTF-8转换为窄的多字节字符表示/将单个码位表示从窄的多字节字符转换为UTF-8
- 无序容器支持异质查找
特性允许调用者传递任何类型的键(只要用户指定的comparator仿函数)。
- execution::unseq
- midpoint
计算整数、浮点数或指针a和b的中点
- lerp
计算a和b之间的线性插值,如果参数t在[0,1]之内(否则为线性外推),则结果为a+t(b-a),需要考虑浮点计算不精确。
- 统一的容器删除
erase/erase_if