C++11 关键字使用概述

一. C++11关键字概述

基本逻辑控制
▪ if, else, for, do, while, return, break, continue, goto, switch, case, default
异常控制
▪ try, catch, throw, static_assert[编译时的静态断言], noexcept[动态异常判断(模版有用)]
▪ *_cast [const_cast, dynamic_cast,static_cast, reinterpret_cast 类型转换增强了类型安全性]
▪ decltype[编译时推断类型]
变量类型修饰
▪ const, constexpr, mutable [修饰非静态非const数据成员,表该成员可在const函数中使用]
▪ volatile, register, extern, auto
函数修饰
▪ virtual, friend, inline, operator, static, explicit [修饰构造函数]
▪ private, protected, public
定义类型
▪ struct, union, enum, class, typedef, typeid[获取表达式的类型], namespace, using
▪ template, typename
实体变量
▪ this, nullptr
▪ new, delete, sizeof

▪ asm
▪ alignas, alignof
▪ thread_local
▪ export [编译效率低,取消]

注意和 std::atomic 的区别
std::shared_ptr, std::auto_ptr, std::weak_ptr, std::make_shared

二. C++11关键字使用详细说明

2.1 const 和 constexpr 的区别:

语义上:
constexpr:告诉编译器我可以是编译期间可知的,尽情的优化我吧。
const:告诉程序员没人动得了我,放心的把我传出去;或者放心的把变量交给我,我啥也不动就瞅瞅。
语法上:
constexpr是一种比const 更严格的束缚, 它修饰的表达式本身在编译期间可知, 并且编译器会尽可能的
evaluate at compile time.在constexpr 出现之前, 可以在编译期初始化的const都是implicit constexpr.
直到c++ 11, constexpr才从const中细分出来成为一个关键字, 而 const从1983年 c++ 刚改名的时候就存在了。
使用案例:类中直接声明与定义 static constexpr int scaleBarWidth = 20; 编译时会自动替换。

三. C++的基本特性

string: move语义、写时拷贝+引用计数特性.
implicit sharing机制:STL与qt的区别,和STL中的引用计数?

构造复制构造移动构造赋值操作移动操作的说明:
A() 构造函数
A(const A& a) 复制构造
A(A&& a) 移动构造
A& operator=(const A& other) 赋值操作
A& operator=(A&& a) { return *this; }移动操作

std::atomic类:std::atomic
成员函数:
is_lock_free 检查原子对象是否免锁、store 原子地以非原子对象替换原子对象的值、load、exchange
特化成员函数:
fetch_add原子地将参数加到存储于原子对象的值、fetch_sub、operator++原子地自增或自减当前值、operator–

发布了41 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wade_510/article/details/104275470