Skip to content

C++ Standard and Compilors

Goals

  • 理解,可以说出C++11 - C++26 的feature, 以及可以写出应用

  • C++11: SmartMMoveALambdaForNullptr

  • C++14:

constexpr auto[x=1](auto x) {
  exchange(make_unique(integer_seq));
}
  • C++17: (IVOFFT)inline if cons ;variant; Filesys; Fold; optional;This lambda

  • C++20: (CCS FMRJ)concept ;ranges; module; corutine; format; jthread;spaceship

  • C++23: if consteval; print; [i,j]; import std; expected;

  • C++26: Static Reflection;meta; contracts; debugging

C++ Standard

C++11

https://en.cppreference.com/w/cpp/11.html

auto 类型推导

auto 让编译器根据初始化表达式自动推导变量的类型, auto的推导遵循与函数模板类型推导(template type deduction)相同的规则。

Move Semantics

移动语义(Move Semantics)和右值引用(Rvalue References)

Smart Pointer

智能指针(std::unique_ptr, std::shared_ptr)

Lambda

Lambda 表达式

inheriting constructors

range-based for

for (auto x : vec)

nullptr

引入了专门的空指针常量,取代 NULL, 是type safe的, 避免和int重载歧义

Memory Model

保证多线程行为一致,不再依赖平台或编译器

Uniform Initialization

{}

constexpr

decltype

模板别名

usingusing Vec = std::vector<T>;

变长模板参数(Variadic Templates)

多线程库

<thread>, <mutex>, <future>

C++14

make_unique

  • std::integer_sequence / std::index_sequence
  • 泛化 constexpr(Generalized constexpr)
  • Return Type Deduction
  • Lambda Capture Expressions
  • 泛型 Lambda(Generic Lambdas)

std::exchange

C++17

https://en.cppreference.com/w/cpp/17.html

  • 结构化绑定(Structured Bindings) auto [x, y, z] = std::make_tuple(1, 2, 3);
  • Mandated Copy Elision 强制省略复制
  • ifswitch 中声明局部变量
  • 内联变量(inline variables)
  • 折叠表达式(Fold Expressions)
  • constexpr if(编译期条件)
  • Lambda 可以按值捕获当前对象副本
  • 使用 alignasalignof 关键字控制/查询内存对齐方式
  • std::optional
  • std::variant
  • std::string_view
  • std::filesystem
  • 可以给算法(如 std::sort)加执行策略(如并行化):
  • std::scoped_lock
  • std::shared_mutex

C++20

  • Concepts(概念)
  • spaceship operator, <utility>
  • Ranges 库
  • Coroutines(协程)
  • Modules(模块)
  • std::format
  • std::jthread
  • std::to_array

C++23

  • print
  • multidimensional subscript operator

  • if consteval

  • 多维下标运算符(multidimensional subscript operator)
  • [[assume(expr)]] 属性
  • std::expected<T, E>

安装g++14, debian

echo 'deb http://deb.debian.org/debian unstable main' | sudo tee /etc/apt/sources.list.d/debian-unstable.list
sudo apt -t unstable install gcc-14 g++-14

C++26

  • <numeric> satuation value wrapper.

C++ Compilors

GCC

Linux

Clang

编译错误信息 清晰、易读(比 GCC 友好很多)

MSVC

Windows SDK / .NET / DirectX / MFC 等完美集成