C++ 11/14/17主要特性
C++11 主要特性概念(含英文对照)
中文特性 | 概念定义 | English Feature | Definition |
自动类型推断 (auto) | 编译器根据初始化表达式自动推断变量类型,无需显式指定类型。 | Auto Type Deduction | Compiler deduces variable type automatically from initializer. |
范围for循环 | 基于范围的循环语法,简化数组和容器的遍历。 | Range-based for loop | Simplified syntax for iterating over containers or arrays. |
nullptr 关键字 | 表示空指针,类型安全替代 NULL。 | nullptr keyword | Type-safe null pointer literal replacing NULL. |
右值引用和移动语义 | 引入 &&,支持资源的转移以提升性能,减少不必要复制。 | Rvalue references & Move semantics | Enables efficient transfer of resources by moving instead of copying. |
Lambda 表达式 | 匿名函数对象,可以在代码中内联定义函数逻辑。 | Lambda expressions | Anonymous functions defined inline for concise code. |
constexpr 函数 | 可在编译期求值的常量表达式函数,提高性能。 | constexpr functions | Functions evaluable at compile time to optimize performance. |
静态断言 (static_assert) | 编译期断言条件是否满足,不满足则报错。 | static_assert | Compile-time assertion checking conditions during compilation. |
强类型枚举 (enum class) | 定义作用域和类型安全的枚举类型,避免名称冲突。 | Scoped enum (enum class) | Scoped and type-safe enumeration to prevent naming conflicts. |
委托构造函数 | 允许一个构造函数调用同类中另一个构造函数,减少代码重复。 | Delegating constructors | Constructors that call other constructors in the same class. |
默认和删除函数 (=default, =delete) | 显式声明默认或删除某些函数,控制函数的自动生成。 | Defaulted and deleted functions | Explicitly default or delete special member functions. |
统一初始化语法 | 使用花括号 {} 进行统一的变量初始化,支持列表初始化。 | Uniform initialization | Brace initialization syntax for consistent object initialization. |
变长模板参数 | 模板支持任意数量参数的定义和展开。 | Variadic templates | Templates accepting variable number of template arguments. |
内联命名空间 | 支持版本化和符号重载的命名空间,减少名称冲突。 | Inline namespaces | Namespaces that can be versioned and merged inline. |
用户自定义字面量 | 自定义字面量语法,实现自定义类型的字面量转换。 | User-defined literals | Defining custom literal suffixes for user types. |
线程库 | 标准线程和同步原语支持,实现多线程编程基础。 | Thread library | Support for threads and synchronization primitives. |
智能指针 | 新增 unique_ptr, shared_ptr, weak_ptr 管理动态内存。 | Smart pointers | RAII-style pointers for automatic memory management. |
移动构造和移动赋值 | 支持移动语义的构造函数和赋值操作符,提高性能。 | Move constructors & assignments | Constructors/operators that move resources instead of copying. |
内存模型和原子操作 | 定义多线程内存访问顺序和原子操作,保障线程安全。 | Memory model and atomics | Defines memory ordering and atomic operations for concurrency. |
C++14 主要特性概念(含英文对照)
中文特性 | 概念定义 | English Feature | Definition |
泛型 Lambda | Lambda 参数支持自动类型推断,使写法更简洁灵活。 | Generic lambdas | Lambdas with auto-typed parameters for generic code. |
constexpr 函数增强 | constexpr 函数支持更多语句和更复杂逻辑。 | Relaxed constexpr functions | constexpr functions can contain more complex code. |
变量模板 | 模板不仅限于函数和类,也支持变量模板定义。 | Variable templates | Templates for variables enabling generic constant definitions. |
二进制字面量和数字分隔符 | 支持二进制数字表示和数字间下划线分隔,提高数字字面量可读性。 | Binary literals & digit separators | Binary numeric literals and underscore separators for readability. |
函数返回类型自动推断 | 普通函数支持根据返回语句自动推断返回类型,无需显式写明。 | Return type deduction | Automatic deduction of function return types. |
decltype(auto) | 增强类型推导,自动推断表达式的准确类型(包含引用与常量性)。 | decltype(auto) | Deduces exact type including references and cv-qualifiers. |
std::make_unique | 提供安全创建 unique_ptr 的工厂函数,防止裸指针泄漏。 | std::make_unique | Factory function to safely create unique_ptr instances. |
std::shared_timed_mutex | 支持多读单写的共享互斥锁。 | std::shared_timed_mutex | Shared mutex allowing multiple readers or one writer lock. |
元编程工具 | 引入 std::integer_sequence 等模板工具简化模板元编程。 | Metaprogramming utilities | Tools like integer_sequence to simplify template metaprogramming. |
C++17 主要特性概念(含英文对照)
中文特性 | 概念定义 | English Feature | Definition |
结构化绑定声明 | 允许用多个变量绑定元组或结构体的多个成员,简化拆包操作。 | Structured bindings | Binding multiple variables to tuple/struct elements. |
if constexpr | 编译期条件语句,根据常量条件选择代码分支,未选分支不生成代码。 | if constexpr | Compile-time conditional branching in templates. |
内联变量 | 支持定义 inline 变量避免多重定义,便于头文件中常量定义。 | Inline variables | Variables declared inline to avoid multiple definitions. |
折叠表达式 | 支持模板参数包的折叠计算,简化变参模板编写。 | Fold expressions | Expression folds to combine parameter packs easily. |
类模板参数推导 (CTAD) | 支持根据构造函数参数自动推导类模板参数类型,减少显式指定。 | Class template argument deduction | Automatic deduction of class template parameters. |
std::optional | 表示可能有值或无值的对象类型,替代空指针或特殊值。 | std::optional | Wrapper representing optional values. |
std::variant | 类型安全的联合体,能存储多种可能类型之一。 | std::variant | Type-safe union holding one of multiple types. |
std::any | 类型擦除容器,可存储任意类型的值。 | std::any | Type-erased container for single values of any type. |
并行算法 | 标准算法支持并行执行策略,提高性能。 | Parallel algorithms | Algorithms supporting parallel execution policies. |
文件系统库 (<filesystem>) | 标准化跨平台文件和目录操作的API。 | Filesystem library | Standardized API for file and directory management. |
字符串视图 (std::string_view) | 轻量级、非拥有的字符串视图,避免复制字符串数据。 | std::string_view | Lightweight non-owning string reference type. |
内存模型增强 | 对原子操作和内存顺序支持增强,提升多线程安全。 | Enhanced memory model | Improved support for atomic operations and memory ordering. |