系列文章目录
文章目录
前言
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 |
使用场景
- 函数可能无返回值
- 避免特殊标记
- std::optional 提供了一种类型安全的方式处理"可能有值"的逻辑,避免了传
统方法的潜在错误。
C++23
- std::expected<T, E>
核心概念
std::expected<T, E> 是一个模板类,包含两个可能的成员:
期望值(Value):类型为 T 的有效结果。
错误值(Error):类型为 E 的错误信息。
通过明确的类型区分,它强制开发者显式处理可能的错误,避免忽略错误检查的风险。
主要特性
- 类型安全的错误处理
替代传统的错误码(如返回 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. 替代异常
在禁用异常的环境(如嵌入式系统)中,提供类型安全的错误传递机制。
- 链式操作
通过 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};
};
总结
无