1、函数返回值类型推导

c++14对函数返回类型推导规则做了优化:

auto func(int i) {  //C++11编译非法,c++14支持auto返回值类型推导
return i;
}
int main() {
cout << func(4) << endl;
return 0;
}

支持函数模块返回值类型推导:

template<typename T>
auto func(T t) { return t; } int main() {
cout << func(4) << endl;
cout << func(3.4) << endl;
return 0;
}

auto返回值用例:

// 编译失败,多个return语句必须返回相同类型。
auto func(bool flag) {
if (flag) return 1;
else return 2.3;
} // 编译失败,不能返回初始化列表
auto func1() {
return {1, 2, 3};
} //虚函数不能返回类型推导
class A{
virtual auto func() { return 1; }
}; //返回值类型推导可以提前前声明,但使用前必须进行函数定义。
auto f(); // declared, not yet defined
auto f() { return 42; } // defined, return type is int // 回类型推导可以用在递归函数中,但是递归调用必须以至少一个返回语句作为先导,以便编译器推导出返回类型。
auto sum(int i) {
if (i == 1)
return i; // return int
else
return sum(i - 1) + i; // ok
} int main() {
cout << f() << endl;
return 0;
}

2、lambda参数auto

在c++11中,lambda表达式参数需要使用具体的类型声明。

auto f = [] (int a) { return a; }

在c++14中,lambda表达式参数可以直接为auto。

auto f = [] (auto a) { return a; };
cout << f(1) << endl;
cout << f(2.3f) << endl;

3、变量模板

c++14支持变量模板。

template<class T>
constexpr T pi = T(3.1415926535897932385L); int main() {
cout << pi<int> << endl; // 3
cout << pi<double> << endl; // 3.14159
return 0;
}

4、别名模板

c++14支持别名模板。

template<typename T, typename U>
struct A {
T t;
U u;
}; template<typename T>
using B = A<T, int>; int main() {
B<double> b;
b.t = 10;
b.u = 20;
cout << b.t << endl;
cout << b.u << endl;
return 0;
}

5、constexpr的限制

1)c++14相较于c++11减少了限制:

c++11和c++14中constexpr函数均可以使用递归。

constexpr int factorial(int n) { // C++14 和 C++11均可
return n <= 1 ? 1 : (n * factorial(n - 1));
}

c++14还可以使用局部变量和循环。

constexpr int factorial(int n) { // C++11中不可,C++14中可以
int ret = 0;
for (int i = 0; i < n; ++i) {
ret += i;
}
return ret;
}

c++11中constexpr函数中必须把所有东西放在一个单独的return语句中。

constexpr int func(bool flag) { // C++14 和 C++11均可
return 0;
}

c++14中constexpr函数没有上述限制。

constexpr int func(bool flag) { // C++11中不可,C++14中可以
if (flag) return 1;
else return 0;
}

6、[[deprecated]]标记

c++14中增加deprecated标记,修饰类、变量、函数等。编译时产生警告,提醒用户该标记修饰的内容未来可能会被丢弃。

struct [[deprecated]] A { };

int main() {
A a;
return 0;
}

7、二进制字面量与字面量分隔符

c++14引入了二进制字面量和字面量分隔符。

int a = 0b0001'0011'1010;
double b = 3.14'1234'1234'1234;

8、std::make_unique

c++11中有std::make_shared,c++14增加了std::make_unique。

struct A {};
std::unique_ptr<A> ptr = std::make_unique<A>();

9、std::shared_timed_mutex与std::shared_lock

c++14通过std::shared_timed_mutex和std::shared_lock来实现读写锁,保证多个线程可以同时读,但是写线程必须独立运行,写操作和读操作不可同时进行,这种情况下才能从shared_mutex中获取性能优势。

c++11 中互斥量

互斥量 说明
std::mutex 独占的互斥量,不能递归使用
std::timed_mutex 有超时能力的独占互斥量,不能递归使用
std::recursive_mutex 递归互斥量
std::recursive_timed_mutex 有超时能力的递归互斥量

c++14互斥量管理类-锁

  • shared_lock是read lock。搭配std::shared_mutex使用,被锁定后允许其它线程执行同样被shared_lock的代码。
  • lock_gurd和unique_lock是write lock。被锁定后,不允许其它线程执行被share_lock或unique_lock的代码。

通常这样定义:

typedef std::shared_lock<std::shared_mutex> ReadLock;
typedef std::lock_guard<std::shared_mutex> WriteLock;

实现方式:

struct ThreadSafe {
mutable std::shared_timed_mutex mutex_;
int value_; ThreadSafe() {
value_ = 0;
} int get() const {
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
return value_;
} void increase() {
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
value_ += 1;
}
};

10、 std::integer_sequence

表示一个编译时的整型序列。

11、std::exchange

以 new_value 替换 obj 的值,并返回 obj 的旧值。注意与std::swap的区别。

vector<int> v{4,5,6,7};
vector<int> x = std::exchange(v, {1,2,3,4});
cout << v.size() << endl;
for (int a : v) {
cout << a << " ";
}
cout<<endl;
for (int a : x) {
cout << a << " ";
}

12、std::quoted

c++14引入std::quoted用于给字符串添加双引号。

string str = "hello world";
cout << str << endl;
cout << std::quoted(str) << endl;
/* 输出
* hello world
* "hello world"
*/

c++14新特性的更多相关文章

  1. C++ 14 新特性总结

    转载自: http://www.codeceo.com/article/cpp-14-new-features.html C++14 这一继C++11 之后的新的 C++ 标准已经被正式批准,正在向 ...

  2. Java 15 正式发布, 14 个新特性,刷新你的认知!!

    JDK 15 2020/09/15 如期而至! 这个时间牛逼啊,和苹果发布会同天? OracleJDK 15 发布地址: https://www.oracle.com/java/technologie ...

  3. Java程序员必备基础:JDK 5-15都有哪些经典新特性

    前言 JDK 15发布啦~ 我们一起回顾JDK 5-15 的新特性吧,大家一起学习哈~ 本文已经收录到github ❝ https://github.com/whx123/JavaHome ❞ 「公众 ...

  4. Java9至17的新特性总结

    总览 讲讲Java 9-17 的一些语法糖和一些新发布的jeps, 重点讲讲JVM的垃圾回收器 时间线 SpringBoot 为什么选择Java17这个版本.我估计跟下面这个图有关系. Java 8 ...

  5. Java 17 新特性:switch的模式匹配(Preview)

    还记得Java 16中的instanceof增强吗? 通过下面这个例子再回忆一下: Map<String, Object> data = new HashMap<>(); da ...

  6. atitit.eclipse 新特性总结3.1--4.3

    atitit.eclipse 新特性总结3.1--4.3 Eclipse 3.1 1 Eclipse 3.2 Java开发工具的新特性 2 1. 内容辅助(Ctrl+Space)模板 2 2. 动态地 ...

  7. 转:关于C++14:你需要知道的新特性

    关于C++14:你需要知道的新特性 遇见C++ Lambda C++14 lambda 教程 C++11 lambda表达式 C++标准库:使用 std::for_each std::generate ...

  8. C++11 & C++14 & C++17新特性

    C++11:C++11包括大量的新特性:包括lambda表达式,类型推导关键字auto.decltype,和模板的大量改进. 新的关键字 auto C++11中引入auto第一种作用是为了自动类型推导 ...

  9. Atitit eclipse新特性总结3.1---4.4  4.5

    Atititeclipse新特性总结3.1---4.4  4.5 1. Eclipse 4.4 Luna正式发布了.1 1.1. 新版本的Eclipse默认对Java8提供支持1 1.2. 内存分析器 ...

随机推荐

  1. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 【LeetCode】811. Subdomain Visit Count 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...

  3. CODEFORCEs 621E. Wet Shark and Blocks

    E. Wet Shark and Blocks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. DAG-GNN: DAG Structure Learning with Graph Neural Networks

    目录 概 主要内容 代码 Yu Y., Chen J., Gao T. and Yu M. DAG-GNN: DAG structure learning with graph neural netw ...

  5. 对vector和map容器的删除元素操作

    /** * 删除头部元素 * 切割map到指定的个数 * @param map * @param i * @return */ map<int, Rect> PublicCardFrame ...

  6. CS5265/CS5267设计替代VL102+PS176 Typec转HDMI2.0音视频芯片

    目前USB TYPEC转HDMI2.0转换方案或者TYPEC转HDMI2.0转换器方案都是用PS176加一个PD芯片来实现,其中VL102是一颗PD协议芯片,PS176是一款DP转HDMI2.0视频解 ...

  7. Oracle对表空间、用户、用户权限的操作

    一.对表空间的操作 1.创建表空间(create tablespace) -- 'hpdb_tablespace' 指定表空间名称 -- 'e:\hpdb.dbf' 指定表空间数据文件名称 -- si ...

  8. Exchange ProxyLogon漏洞分析

    Exchange ProxyLogon漏洞分析 前言 续前文继续学习Exchange漏洞 Proxyshell 影响范围 Exchange Server 2019 < 15.02.0792.01 ...

  9. CSS基础 overflow 内容溢出部分显示效果

    属性:overflow 值 作用 visible 默认,内容溢出部分可见 hidden 内容溢出部分不可见 scroll 内容有无溢出,都有滚动条 auto 有内容溢出,自动显示滚动条

  10. [ vue ] 解耦vuex(按照组件来组织vuex的结构)

    问题描述 随着应用复杂度的增加,vuex用一个 store/index.js 文件来描述已经很难维护了,我们想把这些状态分割到单独文件里面. 参考1:https://vuex.vuejs.org/zh ...