现在的位置: 首页 > 综合 > 正文

了解C++11(一)—— C++11的新特性

2014年09月02日 ⁄ 综合 ⁄ 共 2414字 ⁄ 字号 评论关闭

第一份c++标准在1998年制定(c++98),之后WG21(c++标准委员会)经过十多年的工作,于2011年终于又完成了一份新标准(c++11)。WG21曾在2003年提交过一份技术勘误表(Technical Corrigendum,简称TC1),主要是对c++98进行的缺陷修复,没有修改语言的核心内容。有人把这个版本称为c++03,或者与c++98合在一起称为c++98/03。此外,在2005年还有一份标准的非规范出版物TR1,其中包含了14个可能进入新标准的程序库。

c++11新增了一百多个特性,修补了c++98/03中的600多个缺陷。使用c++11编写代码会更便捷。C++11主要的新特性:

* _cplusplus*对齐支持 alignmentsupport

* 通用属性 generalattribute

* 原子操作 atomicoperation

* auto(初始化类型推导 typededuction from 18nitialize

* C99特性

* 强类型枚举 enum class(scoped and strongly typed enums)

* 复制及再抛出异常 copy andrethrow exception

* 常量表达式 constexpr

* decltype

* 函数的默认模板参数 defaulttemplate parameters for function

* 显式默认和删除的函数(默认的控制)defaultedand deleted functions (control of defaults)

* 委托构造函数 delegatingconstructors

* 并行动态初始化和析构 DynamicInitialization and Destruction with Concurrency

* 显式转换操作符 explicitconversion operators

* 扩展的friend语法

* 扩展的整型 extendedinteger types

* 外部模板 externtemplates

* 一般化的SFINAE规则 generalizedSFINAE rules

* 统一的初始化语法和语义Uniform initialization syntax and semantics

* 非受限联合体unrestricted union

* 用户定义的字面量user-defined literals

* 变长模板 variadictemplates

* 类成员初始化 in-classmember initializers

* 继承构造函数 inheritedconstructors

* 初始化列表 initializerlists

* lambda函数

* 局部类型用作模板参数 localclasses as template arguments

* long long 整型

* 内存模型 memory model

* 移动语义(参见右值引用) movesemanticsseervalue references

* 内联名字空间 Inlinenamespace

* 防止类型收窄 Preventingnarrowing

* 指针空值 nullptr

* PODplain old data

* 基于范围的for语句 range-based forstatement

* 原生字符串字面量 rawstring literals

* 右值引用 rvaluereference

* 静态断言 staticassertions

* 追踪返回类型语法 trailingreturn type syntax

* 模板别名 templatealias

* 线程本地的存储thread-local storage

* Unicode

* 右尖括号Right anglebracket

此外,c++11还对标准库进行了增强:

* 算法增强Algorithmimprovements

* 容器增强Containerimprovements

* 分配算符Scopedallocators

* std::array

* std::forward_list

* 无序容器Unorderedcontainers

* sts::tuple

* 类型特性Type traits

* std::function,std::bind

* unique_ptr

* shared_ptr

* weak_ptr

* 线程Threads

* 互斥Mutex

* Locks

* 条件变量Conditionvariables

* 时间工具Time utilities

* std::future,std::promises

* std::async

* 随机数Random numbers

* 正则表达式regex

C++11的新关键字如下:

* alignas

* alignof decltype

* auto(重新定义)

* static_assert

* using(重新定义)

* noexcept

* export(弃用,未来可能留作他用)

* nullptr

* constexpr

* thread_local

* override

* final

WG21在设计c++11时专注于以下理念:

* 保持语言的稳定性和兼容性(Maintainstability and compatibility

* 更倾向于使用库而不是扩展语言来实现特性(Preferlibraries to language extensions

* 更倾向于通用的而不是特殊的手段来实现特性(Prefergenerality to specialization

* 专家新手一概支持(Supportboth experts and novices

* 增强类型的安全性(Increase typesafety

* 增强代码执行性能和操作硬件的能力(Improveperformance and ability to work directly

with hardware

* 开发能够改变人们思维方式的特性(Makeonly changes that change the way people

think

* 融入编程现实(Fit intothe real world

学习资料: 《深入理解C++11:新特性解析与应用》

抱歉!评论已关闭.