Modern C++ 特性小结

系列文章目录


文章目录


前言

The Zen of Python, by Tim Peters


Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than right now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!


C++11

C++11是C++自C++98以来的第二个重要更新。大量更新被引入标准,提升了C++的抽象程度。

1. 诊断库

<exception>

<system_error>

异常处理头文件和系统错误头文件

捕获和存储异常对象

cpp 复制代码
uncaught_exceptions

std::terminate();  // 默认调用std::abort

typedef void ( *terminate_handler )();
// 返回set之前的值
std::terminate_handler set_terminate( std::terminate_handler f ) noexcept;
std::terminate_handler get_terminate() noexcept;

2. 元编程库

C++提供元编程工具,如 type traits, compile-time rational arithmetic, and compile-time integer sequences.

type traits定义了编译时基于模板的类型属性查询接口。

一元type traits用于检查类型是 std::true_type / std::false_type

<type_traits>

is_void checks if a type is void

is_null_pointer checks if a type is std::nullptr_t

is_integral checks if a type is an integral type

is_floating_point checks if a type is a floating-point type

is_array checks if a type is an array type

is_enum checks if a type is an enumeration type

is_union checks if a type is a union type

is_class checks if a type is a non-union class type

is_function checks if a type is a function type

is_pointer checks if a type is a pointer type

is_lvalue_reference checks if a type is an lvalue reference

is_rvalue_reference checks if a type is an rvalue reference

is_member_object_pointer checks if a type is a non-static member object pointer

is_member_function_pointer checks if a type is a non-static member function pointer

3. 通用工具库

4. 容器库

5.

C++17

std::optional

cpp 复制代码
std::optional<int> optInt;          // 默认不包含值
std::optional<std::string> optStr = "Hello"; // 直接初始化
std::optional<double> optDouble = std::nullopt; // 显式表示无值
Column 1 Column 2
has_value() 检查是否包含值 (bool 类型)
value() 获取值,若无值则抛出 std::bad_optional_access
value_or(T default) 返回值或默认值
operator* 解引用获取值(需确保有值)
operator-> 访问成员(如 opt->size())
reset() 清除值,变为 std::nullopt

使用场景

  1. 函数可能无返回值
  2. 避免特殊标记
  3. std::optional 提供了一种类型安全的方式处理"可能有值"的逻辑,避免了传
    统方法的潜在错误。

C++23

- std::expected<T, E>

核心概念

https://github.com/martinmoene/expected-lite

std::expected<T, E> 是一个模板类,包含两个可能的成员:

​​期望值​​(Value):类型为 T 的有效结果。

​​错误值​​(Error):类型为 E 的错误信息。

通过明确的类型区分,它强制开发者显式处理可能的错误,避免忽略错误检查的风险。

主要特性​

  1. 类型安全的错误处理
    替代传统的错误码(如返回 int 错误码)或异常(try/catch),提供编译时的类型检查。
    错误类型 E 可以是自定义类型(如枚举、结构体),增强错误信息的表达能力。
Column 1 Column 2
has_value() 检查是否包含有效值(返回 bool)。
operator-> 访问有效值的成员(仅在包含有效值时可用)。
operator* 解引用有效值(仅在包含有效值时可用)。
value() 返回有效值,若无值则抛出 std::bad_expected_access。
error() 返回错误值,若存在有效值则抛出 std::bad_expected_access。

使用场景

​​1. 替代错误码​​

避免函数返回歧义的错误码(如 -1 或 nullptr),明确区分成功和失败的结果。

​2. ​替代异常​​

在禁用异常的环境(如嵌入式系统)中,提供类型安全的错误传递机制。

  1. ​​链式操作​​

通过 transform 和 and_then 实现函数式编程风格的链式调用,简化错误处理逻辑。

- std::scope_exit

cpp 复制代码
template<typename EF>
class scope_exit
{
  public:
    explicit scope_exit(EF&& fn): exit_function_(std::forward<EF>(fn) {}
   	...
    ~scope_exit()
    {
        if (!flag_) exit_function_();
        flag_ = true;
    }
    void cancelle() { flag_= true; }
  private:
    EF exit_function_;
    bool flag_{false};
};

总结

相关推荐
honiiiiii2 小时前
2026 SMU week1
c++
yi.Ist2 小时前
关于若干基础的几何问题
c++·学习·算法·计算几何
Whisper_Sy2 小时前
Flutter for OpenHarmony移动数据使用监管助手App实战 - 周报告实现
开发语言·javascript·网络·flutter·php
Sylvia-girl2 小时前
线程的死锁【了解】
java·开发语言·jvm
Elias不吃糖2 小时前
java开发的三层架构
java·开发语言·架构
hetao17338373 小时前
2026-01-22~23 hetao1733837 的刷题笔记
c++·笔记·算法
养海绵宝宝的小蜗3 小时前
Python第二次作业
开发语言·python
王燕龙(大卫)3 小时前
linuxptp时间同步
c++
郝学胜-神的一滴3 小时前
深入理解Linux套接字(Socket)编程:从原理到实践
linux·服务器·开发语言·网络·c++·程序人生·算法