学习目标
- 掌握 C++20/23 新特性(
constexpr
、concepts
、Ranges
、Coroutines
) - 理解 模板元编程 的基础思想
- 熟悉 工程构建与依赖管理(CMake 进阶、vcpkg、Conan)
- 具备 跨平台开发能力(Linux / Windows / macOS)
- 遵循 代码规范(Google C++ Style Guide、clang-format、clang-tidy)
- 掌握 CI/CD 与团队协作实践
- 在大型项目中独立负责模块,符合企业工程化用人标准
职业价值:从“能写功能”过渡到“能做工程”,具备企业级开发能力。
1. C++20/23 新特性
现代 C++ 的演进重点在于 更强的抽象能力 和 更高的可读性。
1.1 constexpr
(编译期计算)
constexpr
允许在编译期执行函数,提高性能并减少运行时开销。
#include <iostream>
using namespace std;
constexpr int square(int x) {
return x * x;
}
int main() {
constexpr int val = square(5); // 编译期计算
cout << val << endl;
}
这里 square(5)
在编译时就被计算为 25
。
1.2 concepts
(约束模板参数)
concepts
用于限制模板参数类型,使模板代码更清晰。
#include <concepts>
#include <iostream>
using namespace std;
template <typename T>
concept Number = requires(T x) { x + x; };
template <Number T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(3, 4) << endl; // OK
// cout << add("a", "b"); // 编译错误
}
这里 add
只能接受支持 +
运算的类型。
1.3 Ranges(更优雅的集合操作)
ranges
提供了更简洁的集合操作方式。
#include <ranges>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6};
for (int x : v | ranges::views::filter([](int n){ return n % 2 == 0; })) {
cout << x << " "; // 输出 2 4 6
}
}
相比传统 std::copy_if
,ranges
更直观。
1.4 协程(Coroutines)
协程让异步编程更自然。
#include <coroutine>
#include <iostream>
using namespace std;
struct Task {
struct promise_type {
Task get_return_object() { return {}; }
suspend_never initial_suspend() { return {}; }
suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
Task hello() {
cout << "Hello ";
co_await suspend_always{};
cout << "World\n";
}
int main() {
auto t = hello();
cout << "Coroutine started\n";
}
协程适合网络编程、游戏逻辑等需要异步的场景。
2. 模板元编程基础
模板元编程(TMP)是利用编译期计算实现代码生成的技术。
下面的例子展示了 编译期阶乘:
#include <iostream>
using namespace std;
template<int N>
struct Factorial {
static constexpr int value = N * Factorial<N - 1>::value;
};
template<>
struct Factorial<0> {
static constexpr int value = 1;
};
int main() {
cout << Factorial<5>::value << endl; // 输出 120
}
虽然 TMP 复杂,但它是理解现代库(如 std::ranges
、Boost
)的基础。
3. 工程构建与依赖管理
3.1 CMake 进阶
CMake 是跨平台构建工具,掌握其高级用法是工程化的必备技能。
cmake_minimum_required(VERSION 3.20)
project(MyApp)
set(CMAKE_CXX_STANDARD 20)
add_executable(myapp main.cpp)
# 引入外部库
find_package(fmt REQUIRED)
target_link_libraries(myapp PRIVATE fmt::fmt)
这里我们使用 find_package
引入 fmt
库。
3.2 vcpkg 与 Conan
- vcpkg:微软维护的包管理器,适合 Windows / Linux
- Conan:跨平台依赖管理工具,适合大型项目
示例:用 vcpkg 安装 fmt
:
vcpkg install fmt
然后在 CMake 中:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(myapp PRIVATE fmt::fmt)
4. 跨平台开发
企业级项目往往需要同时支持 Linux、Windows、macOS。
- Linux:常用于服务器端,工具链丰富(gcc, clang, gdb, perf)
- Windows:常用于桌面应用和游戏,VS 工具链强大
- macOS:常用于跨平台开发和移动端工具链
建议:
- 使用 CMake 保持构建系统一致
- 使用 CI 测试不同平台的构建与运行
5. 代码规范与工具
5.1 Google C++ Style Guide
- 命名规范:类名
CamelCase
,变量名snake_case
- 禁止裸指针,优先使用智能指针
- 严格控制头文件依赖
5.2 clang-format
自动格式化代码:
clang-format -i main.cpp
5.3 clang-tidy
静态分析工具,发现潜在 bug:
clang-tidy main.cpp -- -std=c++20
6. CI/CD 与团队协作实践
在团队开发中,CI/CD 是保障质量的关键。
- CI(持续集成):每次提交自动编译和测试
- CD(持续交付/部署):自动部署到测试或生产环境
示例:GitHub Actions 配置多平台构建:
name: C++ CI
on: [push, pull_request]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: sudo apt-get install -y cmake g++ || true
- name: Build
run: cmake -B build && cmake --build build
- name: Run tests
run: ctest --test-dir build
这样可以保证代码在三大平台上都能正常运行。
7. 学习建议与实践
7.1 参与开源项目
- 目标:在大型项目中贡献工程化改进
- 推荐项目:
- LLVM(现代 C++ 特性应用广泛)
- Qt(跨平台 GUI 框架)
- GoogleTest(学习测试与工程化实践)
7.2 实践路径
- 短期:学习并使用 C++20/23 新特性,写小型 demo
- 中期:掌握 CMake 进阶与依赖管理,构建跨平台项目
- 长期:在开源项目中提交工程化改进(如 CI 配置、代码规范修复、跨平台兼容性补丁)
阶段四总结
- 现代特性:掌握
constexpr
、concepts
、ranges
、协程 - 模板元编程:理解编译期计算与泛型编程的结合
- 工程化:熟悉 CMake、vcpkg、Conan,能管理依赖
- 跨平台开发:能在 Linux/Windows/macOS 上构建和调试
- 代码规范:遵循 Google C++ Style Guide,使用 clang 工具保证质量
- CI/CD:能配置自动化构建与测试,适应团队协作
- 实践产出:在大型开源项目中完成一次工程化贡献
完成阶段四后,你将具备 企业级工程实践能力,能够在大型项目中独立负责模块,符合企业工程化用人标准。