Modern-cpp

这篇博客为自己学习现代C++特性的笔记,参考书为《modern-cpp-tutorial-zh-cn》。

语言可用性的强化

常量

nullptr

目的:取代NULL。

原因:编译器会将 NULL 定义为 0 或者 (void*) 0 。

nullptr 的类型为 nullptr_t ,能够隐式的转换为任何指针或成员指针的类型,也能和他们进行相等或者不等的比较。

constexpr

目的:让用户显式地声明函数或对象构造函数在编译期会成为常量表达式。

(C++ 标准中数组的长度必须是一个常量表达式)

1
2
3
4
5
6
int len = 10;
const int len_2 = len + 1;
constexpr int len_2_constexpr = 1 + 2 + 3;

char arr_4[len_2]; //invalid
char arr_4[len_2_constexpr] //valid

此外,constexpr修饰的函数可以使用递归:

1
2
3
constexpr int fibonacci(const int n){
return n == 1 || n == 2 ? 1 : fibonacci(n-1)+fibonacci(n-2);
}

变量及其初始化

if/switch 变量声明强化

可以在 if 里声明临时变量:

1
2
3
4
if(const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
itr != vec.end()) {
*itr = 4;
}

初始化列表

给类对象的初始化与普通数组和POD (Plain Old Data,即没有构造、析构和虚函数的类或结构体)类提供统一使用{}进行初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <initializer_list>
#include <vector>
class MagicFoo{
public:
std::vector<int> vec;
MagicFoo(std::initializer_list<int> list) {
for(std::initializer_list<int>::iterator it = list.begin();
it != list.end(); ++it){
vec.push_back(*it);
}
}
}

int main(){
// after c++11
MagicFoo magic_foo = {1, 2, 3, 4, 5};
}

结构化绑定

有一个临时要用的 tuple, 可以使用结构化绑定简单拿到里面的元素。

1
2
3
4
5
6
7
std::tuple<int, double, std::string> f(){
return std::make_tuple(1, 2.3, "456");
}

int main(){
auto [x, y, z] = f();
}

类型推导

auto

用于替换冗长的迭代写法,让编译器进行类型推导。

1
2
3
4
5
6
auto i = 5;
auto arr = new auto(10);

int add(auto x, auto y){ //c++20 auto用于函数传参
return x+y;
}

注:auto不能用于推导数组类型。

decltype

目的:推导表达式的类型。

1
2
3
auto x = 1;
auto y = 2;
decltype(x+y) z;

尾返回类型推导

auto 用于函数返回类型推导。

1
2
3
4
template<typename T, typename U>
auto add2(T x, U y) -> decltype(x+y){
return x+y;
}

decltype(auto)

decltype(auto) 主要用于对转发函数或封装的返回类型进行推导,它使我们无需显式的指定 decltype 的参数表达式。

1
2
3
4
5
6
7
8
9
std::string lookup1();
std::string& lookup2(); // 封装这两个函数

decltype(auto) look_up_a_string_1() {
return lookup1();
}
decltype(auto) look_up_a_string_2() {
return lookup2();
}

控制流

if constexpr

可以让代码在编译时期就完成分支判断,提高效率。

1
2
3
4
5
6
7
8
template<typename T>
auto print_type_info(const T& t){
if constexpr(std::is_integral<T>::value){
return t + 1;
}else{
return t + 0.001;
}
}

区间for循环

1
2
3
4
5
6
for(auto element : vec){
std::cout<<element<<std::endl; // read only
}
for(auto &element : vec){
element += 1; // write only
}

模板

模板的哲学在于将一切能够在编译期处理的问题丢到编译期进行处理,仅在运行时处理那些最核心的动态服务,进而大幅优化运行期的性能。

外部模板

目的:可以显式地通知编译器何时进行模板的实例化。

原因:在传统c++种,模板只有使用时才会被编译器实例化。如果每个文件中编译的代码都遇到了被完整定义的模板,就会重复实例化,导致编译时间的增加。

1
2
template class std::vector<bool> // 强行实例化
extern template class std::vector<double> // 不在该当前编译文件中实例化模板

尖括号 “>”

1
std::vector<std::vector<int>> matrix;

“>>” 传统一律当成右移, c++11以后合法。

类型别名模板

代替 typedef 原名称 新名称 对函数指针等别名的定义语法。

1
using NewProcess = int(*)(void *);

变长参数模板

目的:允许任意个数、任意类别的模板参数,同时也不需要在定义时将参数的个数固定。

1
2
3
4
5
6
7
8
template<typename... Ts> class Magic; //参数个数可以为0
template<typename Require, typename... Args> class Magic;// 至少一个参数

template<typename T0, typename... T>
void printf2(T0 t0, T... t) {
std::cout << t0 << std::endl;
if constexpr (sizeof...(t) > 0) printf2(t...);
}

折叠表达式

1
2
3
4
5
6
7
8
#include <iostream>
template<typename ... T>
auto sum(T ... t) {
return (t + ...);
}
int main() {
std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
}

面向对象

委托构造

构造函数可以在同一个类中一个构造函数调用另一个构造函数,达到代码简化的目的。

1
2
3
4
5
6
7
8
9
10
11
class Base{
public:
int v1;
int v2;
Base(){
v1 = 1;
}
Base(int v) : Base(){
v2 = v;
}
}

继承构造

1
2
3
4
class Subclass : public Base(){
public:
using Base::Base; //继承构造
}

显式虚函数重载

override

显式地告知编译器进行重载,如果编译器没有检查到这样的虚函数,将无法通过编译。

1
2
3
4
5
6
7
struct Base {
virtual void foo(int);
};
struct Subclass: Base {
virtual void foo(int) override; // valid
virtual void foo(float) override; // invalid
};

final

目的:为了防止类被继续继承以及终止虚函数继续重载。

1
2
3
4
5
6
7
8
9
10
struct Base {
virtual void foo() final;
};
struct Subclass1 final: Base{ // valid
};
struct Subclass2: Subclass1{ // invalid
};
struct Subclass3: Base {
void foo(); // invalid;
}

显式禁用默认函数

1
2
3
4
5
class Magic {
public:
Magic() = default; //显式声明使用编译器生成的构造
Magic& operator=(const Magic&) = delete; // 显式声明拒绝编译器生成构造
}

强类型枚举

1
2
3
4
5
6
enum class new_enum : unsigned int {
value1,
value2,
value3 = 100,
value4 = 100
};

实现了枚举的类型安全。

语言运行期的强化

Lambda表达式

基础

基本语法:

1
2
3
[捕获列表](参数列表) mutable(可选) 异常属性 -> 返回类型 {
// 函数体
}

捕获列表:传递外部数据给内部函数。

  1. 值捕获:前提是变量可以拷贝,被捕获的变量在Lambda表达式被创建时拷贝,而非调用时拷贝。

  2. 引用捕获:保存的是引用,值会发生变化。

  3. 隐式捕获:写一个 & 或者 = 向编译器声明采用引用捕获或者值捕获。

    捕获列表最常用的四种形式:

    • [] 空捕获列表
    • [name1, name2, …] 捕获一系列变量
    • [&] 引用捕获
    • [=] 值捕获
  4. 表达式捕获:(需要了解右值引用以及智能指针)

泛型Lambda

在形参声明中使用auto类型指示说明符的lambda。

1
2
3
auto add = [](auto x, auto y){
return x + y;
}

函数对象包装器

std::function

是一种通用、多态的函数封装,是类型安全的。是函数的容器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <functional>
#include <iostream>

int foo(int para){
return para;
}

int main(){
std::function<int(int)> func = foo;
int impotant = 10;
std::function<int(int)> func2 = [&](int value) -> int {
return 1+value+important;
};
}

std::bind 和 std::placeholder

1
2
3
4
5
6
7
8
9
int foo(int a, int b, int c){
;
}
int main(){
// 将参数1,2绑定到函数foo上, 但是使用std::placeholders::_1来对第一个参数进行占位
auto bindFoo = std::bind(foo, std::placeholders::_1, 1, 2);
// 此时调用 bindFoo时只需要提供第一个参数即可
bindFoo(1);
}

右值引用

左值、右值的纯右值、将亡值、右值

左值:表达式左边的值,表达式后依然存在的持久对象。

右值:表达式右边的值,表达式结束后不再存在的临时对象。

纯右值:要么是纯粹的字面量,比如10, true,要么是求值结果相当于字面量或匿名临时对象,比如 1+2。非引用返回的临时变量、运算表达式产生的临时变量、原始字面量,Lambda表达式都属于纯右值。

注意:字符串字面量只有在类中才是右值,当其位于普通函数中是左值。

将亡值:即将被销毁、却能被移动的值。

1
2
3
4
5
std::vector<int> foo(){
std::vector<int> temp = {1, 2, 3, 4};
return temp;
}
std::vector<int> v = foo();

v把foo()返回的temp复制一份,然后把temp销毁。但是,v 可以被别的变量捕获到,而foo() 产生的那个返回值作为一个临时值,一旦被v 复制后,将立即被销毁,无法获取、也不能修改。而将亡值就定义了这样一种行为:临时的值能够被识别、同时又能够被移动。

右值引用和左值引用

要拿到一个将亡值,需要用到右值引用:T &&,其中T是类型。右值引用的声明让这个临时值的声明周期得以延长。

std::move 将左值参数无条件地转换为右值。

右值引用本身是一个左值。

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string lv1 = "string,"; // lv1 是一个左值
// std::string&& r1 = lv1; // 非法, 右值引用不能引用左值
std::string&& rv1 = std::move(lv1); // 合法, std::move 可以将左值转移为右值
std::cout << rv1 << std::endl; // string,

const std::string& lv2 = lv1 + lv1; // 合法, 常量左值引用能够延长临时变量的生命周期
// lv2 += "Test"; // 非法, 常量引用无法被修改
std::cout << lv2 << std::endl; // string,string,

std::string&& rv2 = lv1 + lv2; // 合法, 右值引用延长临时对象生命周期
rv2 += "Test"; // 合法, 非常量引用能够修改临时变量
std::cout << rv2 << std::endl; // string,string,string,Test
reference(rv2); // 输出左值

移动语义

目的:实现对对象的移动操作,而不是先复制、再析构,从而提高性能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
std::string str = "Hello world.";
std::vector<std::string> v;

// 将使用push_back(cosnt T&),即产生拷贝行为
v.push_back(str);
// 将输出“str: Hello world.”
std::cout<<"str: "<< str <<std::endl;

// 将使用push_back(const T&&),不会出现拷贝行为
// 而整个字符串会被移动到vector中,所以有时候std::move会用来减少拷贝出现的开销
// 这步操作后, str中的值会变空
v.push_back(std::move(str));
// 将输出“str: ”
std::cout<<"str: "<< str <<std::endl;
}

完美转发

std::forward 来进行参数的转发(传递)。

目的:在传递参数的时候,保持原来的参数类型(左引用保持左引用,右引用保持右引用)。

例如右值引用其实是一个左值,但传递的时候也需要保持右值引用,所以需要完美转发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <utility>
void reference(int& v) {
std::cout << " 左值引用" << std::endl;
}
void reference(int&& v) {
std::cout << " 右值引用" << std::endl;
}
template <typename T>
void pass(T&& v) {
std::cout << " 普通传参: ";
reference(v);
std::cout << " std::move 传参: ";
reference(std::move(v));
std::cout << " std::forward 传参: ";
reference(std::forward<T>(v));
std::cout << "static_cast<T&&> 传参: ";
reference(static_cast<T&&>(v));
}
int main() {
std::cout << " 传递右值:" << std::endl;
pass(1);
std::cout << " 传递左值:" << std::endl;
int v = 1;
pass(v);
return 0;
}

结果:
传递右值:
普通传参: 左值引用
std::move 传参: 右值引用
std::forward 传参: 右值引用
static_cast<T&&> 传参: 右值引用
传递左值:
普通传参: 左值引用
std::move 传参: 右值引用
std::forward 传参: 左值引用
static_cast<T&&> 传参: 左值引用

容器

线性容器

std::array

  1. 为什么不用vector?
  2. 为什么不用传统数组?

对于1,array的大小是固定的,vector会自动扩容,扩容以后内存需要手动释放。

对于2,array让代码更“现代化”,可以使用一些封装好的函数以及stl的容器算法。

std::forward_list

是一个列表容器,和std::list类似。使用单向链表实现,插入复杂度是O(1),不支持快速随机访问。当不需要双向迭代时,具有比std::list 更高的空间利用率。

无序容器

有序容器:std::map/std::set 内部通过红黑树实现,插入和搜索平均复杂度为O(logn)。

无序容器:std::unordered_map/std::unordered_multimap和 std::unordered_set/std::unordered_multiset内部通过哈希表实现,内部元素是无序的。

元组

目的:存放不同类型的数据。

元组基本操作

  1. std::make_tuple 构造元组
  2. std::get 获得元组某个位置的值
  3. std::tie 元组拆包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <tuple>
#include <iostream>

auto get_student(int id){
if (id == 0)
return std::make_tuple(3.8, ’A’, " 张三");
if (id == 1)
return std::make_tuple(2.9, ’C’, " 李四");
return std::make_tuple(0.0, ’D’, "null");
}

int main()
{
auto student = get_student(0);
std::cout << "ID: 0, "
<< "GPA: " << std::get<0>(student) << ", "
<< " 成绩: " << std::get<1>(student) << ", "
<< " 姓名: " << std::get<2>(student) << ’\n’;
double gpa;
char grade;
std::string name;
// 元组进行拆包
std::tie(gpa, grade, name) = get_student(1);
std::cout << "ID: 1, "
<< "GPA: " << gpa << ", "
<< " 成绩: " << grade << ", "
<< " 姓名: " << name << ’\n’;
}

//也可以是用类型来获取对象
std::tuple<std::string, double, double, int> t("123", 4.5, 6.7, 8);
std::cout << std::get<std::string>(t) << std::endl;

运行期索引

std::variant<> 可以让一个variant<> 容纳提供的几种类型的变量。variant 可以存放多种类型的数据,但任何时刻最多只能存放其中一种类型的数据。variant 无须借助外力只需要通过查询自身就可辨别实际所存放数据的类型。

元组合并

std::tuple_cat 可以合并两个元组。

1
auto new_tuple = std::tuple_cat(get_student(1), std::move(t));

智能指针与内存管理

RAII和引用计数

RAII(Resource Acquisition Is Initialization):资源获取即初始化技术。

引用计数:为了防止内存泄漏。

std::shared_ptr

std::shared_ptr 是一种智能指针,它能够记录多少个shared_ptr 共同指向一个对象,从而消除显式的调用delete,当引用计数变为零的时候就会将对象自动删除。

使用std::make_shared 来消除显式地调用new,返回这个对象类型的std::shared_ptr指针。

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <memory>

void foo(std::shared_ptr<int> i){
(*i)++;
}

int main(){
auto pointer = std::make_shared<int>(10);
foo(pointer);
std::cout<< *pointer << std::endl;
}

std::shared_ptr 可以通过get() 方法来获取原始指针,通过reset() 来减少一个引用计数,并通过use_count() 来查看一个对象的引用计数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
auto pointer = std::make_shared<int>(10);
auto pointer2 = pointer; // 引用计数+1
auto pointer3 = pointer; // 引用计数+1
int *p = pointer.get(); // 这样不会增加引用计数
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 3
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 3

pointer2.reset();
std::cout << "reset pointer2:" << std::endl;
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 2
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 0, pointer2 已reset
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 2

pointer3.reset();
std::cout << "reset pointer3:" << std::endl;
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 1
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 0
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 0, pointer3 已reset

std::unique_ptr

std::unique_ptr 是一种独占的智能指针,它禁止其他智能指针与其共享同一个对象,从而保证代码的安全。

1
std::unique_ptr<int> pointer = std::make_unique<int>(10);

可以利用std::move将其转移给其他的unique_ptr。

1
std::unique_ptr<Foo> p2(std::move(p1));

std::weak_ptr

std::weak_ptr 是一种弱引用(相比较而言std::shared_ptr 就是一种强引用)。弱引用不会引起引用计数增加。

正则表达式

简介

需求:

  1. 检查一个串是否包含某种形式的子串;
  2. 将匹配的子串替换;
  3. 从某个串中取出符合条件的子串。

正则表达式是由普通字符(例如a 到z)以及特殊字符组成的文字模式。模式描述在搜索文本时要匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。

普通字符

所有大小写字母、所有数字、所有标点符号和一些其它符号。

特殊字符

image-20220722133734739

限定符

image-20220722134926662

std::ragex及其相关

C++11 提供的正则表达式库操作std::string 对象,模式std::regex (本质是std::basic_regex)进行初始化,通过std::regex_match 进行匹配,从而产生std::smatch(本质是std::match_results对象)。

1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <regex>
int main() {
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
for (const auto &fname: fnames)
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << std::endl;
}

并行与并发

并行基础

std::thread 用于创建一个执行的线程实例。

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <thread>

int main(){
std::thread t([](){
std::cout << "hello world!" << std::endl;
});
t.join(); // 加入一个线程
return 0;
}

互斥量与临界区

std::lock_guard是一个RAII语法的模板类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <mutex>
#include <thread>

int v = 1;
void critical_section(int change_v) {
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
// 执行竞争操作
v = change_v;
// 离开此作用域后mtx 会被释放
}
int main() {
std::thread t1(critical_section, 2), t2(critical_section, 3);
t1.join();
t2.join();
std::cout << v << std::endl;
return 0;
}

更好的做法是std::unqiue_lock,会以独占所有权的方式管理mutex。std::lock_guard 不能显式的调用lock 和unlock,而std::unique_lock 可以在声明后的任意位置调用,可以缩小锁的作用范围,提供更高的并发度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void critical_section(int change_v) {
static std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
// 执行竞争操作
v = change_v;
std::cout << v << std::endl;
// 将锁进行释放
lock.unlock();
// 在此期间,任何人都可以抢夺v 的持有权
// 开始另一组竞争操作,再次加锁
lock.lock();
v += 1;
std::cout << v << std::endl;
}

期物

std::future,它提供了一个访问异步操作结果的途径,可以用来获取异步任务的结果。类似于线程同步的手段,屏障(barrier)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <future>
#include <thread>
int main() {
// 将一个返回值为7 的lambda 表达式封装到task 中
// std::packaged_task 的模板参数为要封装函数的类型
std::packaged_task<int()> task([](){return 7;});
// 获得task 的期物
std::future<int> result = task.get_future(); // 在一个线程中执行task
std::thread(std::move(task)).detach();
std::cout << "waiting...";
result.wait(); // 在此设置屏障,阻塞到期物的完成
// 输出执行结果
std::cout << "done!" << std:: endl << "future result is " << result.get() << std::endl;
return 0;
}

条件变量

std::condition_variable 为了解决死锁而引入。condition_variable 实例被创建出现主要就是用于唤醒等待线程从而避免死锁。std::condition_variable 的notify_one() 用于唤醒一个线程;notify_all()则是通知所有线程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <queue>
#include <chrono>
#include <mutex>
#include <thread>
#include <iostream>
#include <condition_variable>
int main() {
std::queue<int> produced_nums;
std::mutex mtx;
std::condition_variable cv;
bool notified = false; // 通知信号
// 生产者
auto producer = [&]() {
for (int i = 0; ; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(900));
std::unique_lock<std::mutex> lock(mtx);
std::cout << "producing " << i << std::endl;
produced_nums.push(i);
notified = true;
cv.notify_all(); // 此处也可以使用notify_one
}
};
// 消费者
auto consumer = [&]() {
while (true) {
std::unique_lock<std::mutex> lock(mtx);
while (!notified) { // 避免虚假唤醒
cv.wait(lock);
}
// 短暂取消锁,使得生产者有机会在消费者消费空前继续生产
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // 消费者慢于生产者
lock.lock();
while (!produced_nums.empty()) {
std::cout << "consuming " << produced_nums.front() << std::endl;
produced_nums.pop();
}
notified = false;
}
};
// 分别在不同的线程中运行
std::thread p(producer);
std::thread cs[2];
for (int i = 0; i < 2; ++i) {
cs[i] = std::thread(consumer);
}
p.join();
for (int i = 0; i < 2; ++i) {
cs[i].join();
}
return 0;
}

原子操作与内存模型

原子操作

std::atomic能够实例化一个原子类型,将一个原子类型读写操作从一组指令,最小化到单个CPU 指令。

1
std::atomic<int> counter;

可以通过std::atomic::is_lock_free来检查该原子类型是否需支持原子操作。

一致性模型

  1. 线性一致性:又称强一致性或原子一致性。它要求任何一次读操作都能读到某个数据的最近一次写
    的数据,并且所有线程的操作顺序与全局时钟下的顺序是一致的。
  2. 顺序一致性:同样要求任何一次读操作都能读到数据最近一次写入的数据,但未要求与全局时钟的
    顺序一致。
  3. 因果一致性:它的要求进一步降低,只需要有因果关系的操作顺序得到保障,而非因果关系的操作
    顺序则不做要求。
  4. 最终一致性:是最弱的一致性要求,它只保障某个操作在未来的某个时间节点上会被观察到,但并
    未要求被观察到的时间。

内存顺序

C++11 为原子操作定义了六种不同的内存顺序std::memory_order 的选项,表达了四种多线程间的同步模型:

  1. 宽松模型:在此模型下,单个线程内的原子操作都是顺序执行的,不允许指令重排,但不同线程间原子操作的顺序是任意的。类型通过std::memory_order_relaxed 指定。
  2. 释放/消费模型:在此模型中,我们开始限制进程间的操作顺序,如果某个线程需要修改某个值,但另一个线程会对该值的某次操作产生依赖,即后者依赖前者。
  3. 释放/获取模型:在此模型下,我们可以进一步加紧对不同线程间原子操作的顺序的限制,在释std::memory_order_release 和获取std::memory_order_acquire 之间规定时序,即发生在释放(release)操作之前的所有写操作,对其他线程的任何获取(acquire)操作都是可见的,亦即发生顺序(happens-before)。
  4. 顺序一致模型:在此模型下,原子操作满足顺序一致性,进而可能对性能产生损耗。可显式的通过std::memory_order_seq_cst 进行指定。

一些杂项

新类型

long long int

至少具备64位的比特数。

noexcept的修饰和操作

C++11 将异常的声明简化为以下两种情况:

  1. 函数可能抛出任何异常
  2. 函数不能抛出任何异常
1
2
void may_throw(); // 可能抛出异常
void no_throw() noexcept; // 不能抛出异常

使用 noexcept 修饰过的函数如果抛出异常,编译器会使用 std::terminate() 来立即终止程序运行。

字面量

原始字符串字面量

可以在一个字符串前方使用R 来修饰这个字符串,同时,将原始字符串使用括号包裹。

1
2
3
4
5
6
7
#include <iostream>
#include <string>
int main() {
std::string str = R"(C:\File\To\Path)";
std::cout << str << std::endl;
return 0;
}