资讯动态

第五章 C++20标准的改进(代码说明)

发布时间:2026/10/18 13:56:11 来源:尧图企业网站定制
// cpp20_demo.cpp// 编译: g -stdc20 -pthread cpp20_demo.cpp -o cpp20_demo// 运行: ./cpp20_demo#include iostream // 输入输出#include format // C20 格式化库#include concepts // C20 概念#include ranges // C20 范围库#include span // C20 span#include vector // 动态数组#include string // 字符串#include array // 固定数组#include algorithm // 算法#include compare // 三路比较#include coroutine // 协程支持#include optional // 可选值#include numbers // 数学常量// // 全局辅助// templatetypename T // 定义概念concept Addable requires(T a, T b) { // 要求 a b 合法{ a b } - std::same_asT; // 且返回类型是 T};templateAddable T // 用概念约束T add(const T a, const T b) { // 通用加法return a b; // 返回结果}struct Point { // 三路比较int x, y; // 坐标auto operator(const Point) const default; // 自动生成所有比较};struct WindowConfig { // 指定初始化器int width 800; // 默认宽int height 600; // 默认高std::string title Untitled; // 默认标题};consteval int compile_time_square(int x) { // 必须编译期求值return x * x; // 返回平方}constinit int global_init compile_time_square(5); // 编译期初始化templatetypename T // 生成器模板struct Generator { // 协程生成器struct promise_type { // 协程承诺类型T current_value; // 当前值auto get_return_object() { // 创建返回对象return Generator{ std::coroutine_handlepromise_type::from_promise(*this) };}auto initial_suspend() { return std::suspend_never{}; } // 立即开始auto final_suspend() noexcept { return std::suspend_always{}; } // 结束保持挂起void return_void() {} // 无返回值void unhandled_exception() { std::terminate(); } // 异常终止auto yield_value(T value) { // co_yield 处理current_value value; // 保存值return std::suspend_always{}; // 暂停等待恢复}};std::coroutine_handlepromise_type handle; // 协程句柄explicit Generator(std::coroutine_handlepromise_type h) : handle(h) {} // 构造Generator(const Generator) delete; // 禁止拷贝Generator operator(const Generator) delete; // 禁止拷贝赋值Generator(Generator o) noexcept : handle(o.handle) { o.handle nullptr; } // 移动构造Generator operator(Generator o) noexcept { // 移动赋值if (this ! o) { // 防止自赋值if (handle) handle.destroy(); // 销毁旧句柄handle o.handle; // 接管新句柄o.handle nullptr; // 置空源对象}return *this; // 返回自身}~Generator() { if (handle) handle.destroy(); } // 析构销毁bool next() { // 恢复协程if (!handle || handle.done()) return false; // 已结束handle.resume(); // 恢复执行return !handle.done(); // 是否还有值}T value() const { return handle.promise().current_value; } // 获取当前值};Generatorint count_up_to(int max) { // 生成 1..maxfor (int i 1; i max; i) // 循环co_yield i; // 产出并暂停}void print_span(std::spanconst int data, std::string_view label) { // span 打印std::cout label : ; // 提示for (int x : data) std::cout x ; // 遍历std::cout \n; // 换行}// // main// int main() { // 程序入口std::cout 1. 概念:\n; // 提示std::cout add(3,4) add(3, 4) \n; // intstd::cout add(1.5,2.5) add(1.5, 2.5) \n; // doublestatic_assert(Addableint); // 编译期检查std::cout 2. 三路比较:\n; // 提示Point p1{1, 2}, p2{3, 4}; // 两个点std::cout p1 p2: (p1 p2) \n; // 自动生成std::cout 3. 指定初始化器:\n; // 提示WindowConfig cfg2{ .width 1024, .height 768 }; // 指定部分成员std::cout cfg2.width x cfg2.height cfg2.title \n; // 输出std::cout 4. consteval / constinit:\n; // 提示constexpr int sq compile_time_square(6); // 编译期std::cout sq sq , global_init global_init \n; // 输出std::cout 5. std::format:\n; // 提示std::cout std::format({} 的年龄是 {}\n, Alice, 30); // 基本std::cout std::format({:.2f}\n, 95.5); // 精度std::cout std::format(|{:10}|{:10}|{:^10}|\n, left, right, center); // 对齐std::cout 6. std::span:\n; // 提示int carr[] {1, 2, 3, 4, 5}; // C 数组std::vectorint vec {10, 20, 30}; // vectorstd::arrayint, 4 arr {100, 200, 300, 400}; // arrayprint_span(carr, C array); // 打印print_span(vec, vector); // 打印print_span(arr, array); // 打印std::cout 7. Ranges / Views:\n; // 提示std::vectorint nums {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 数据源std::cout views: ; // 提示auto result nums // 从 nums 开始| std::views::filter([](int x) { return x % 2 0; }) // 筛选偶数| std::views::transform([](int x) { return x * x; }) // 平方| std::views::take(3); // 取前 3for (int x : result) std::cout x ; // 输出std::cout \n; // 换行std::cout reverse: ; // 提示for (int x : nums | std::views::reverse | std::views::take(3)) // 反转取前 3std::cout x ; // 输出std::cout \n; // 换行std::cout 8. 协程:\n; // 提示auto gen count_up_to(5); // 创建生成器std::cout coroutine: ; // 提示while (gen.next()) { // 逐个恢复std::cout gen.value() ; // 输出当前值}std::cout \n; // 换行std::cout 9. 数学常量:\n; // 提示std::cout std::format(pi {:.10f}\n, std::numbers::pi); // πstd::cout std::format(e {:.10f}\n, std::numbers::e); // estd::cout \n全部 C20 示例运行完毕。\n; // 结束提示return 0; // 正常返回}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑