C++14 新特性总结

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210813002816659.png

1 函数返回值类型推导

C++14对函数返回类型推导规则做了优化,先看一段代码:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/

#include <iostream>

using namespace std;

auto func(int i) {
    return i;
}

int main() {
    cout << func(4) << endl;
    return 0;
}

编译

g++ test01.cpp -std=c++11

上面的代码使用C++11是不能通过编译的,通过编译器输出的信息也可以看见这个特性需要到C++14才被支持。

返回值类型推导也可以用在模板中:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

template<typename T> auto func(T t) { return t; }

int main() {
    cout << func(4) << endl;
    cout << func(3.4) << endl;
    return 0;
}

结果

注意

(1)函数内如果有多个return语句,它们必须返回相同的类型,否则编译失败。

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

auto func(bool flag) {
    if (flag) return 1;
    else return 2.3; // error
}
// inconsistent deduction for auto return type: ‘int’ and then ‘double’



int main() {
    return 0;
}

结果

(2)如果return语句返回初始化列表,返回值类型推导也会失败

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

auto func() {
    return {1, 2, 3}; // error returning initializer list
}


int main() {
    
    return 0;
}

结果

(3)如果函数是虚函数,不能使用返回值类型推导

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

struct A {
    // error: virtual function cannot have deduced return type
    virtual auto func() { return 1; } 
};

int main() {
   
    return 0;
}

结果

(4)返回类型推导可以用在前向声明中,但是在使用它们之前,翻译单元中必须能够得到函数定义

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

auto f();               // declared, not yet defined
auto f() { return 42; } // defined, return type is int

int main() {
    cout << f() << endl;
}

结果

(5)返回类型推导可以用在递归函数中,但是递归调用必须以至少一个返回语句作为先导,以便编译器推导出返回类型。

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

auto sum(int i) {
    if (i == 1)
        return i;              // return int
    else
        return sum(i - 1) + i; // ok
}



int main() {
    cout << sum(10) << endl;
}

结果

2 lambda参数auto

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

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;


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

}

结果

在C++14中,对此进行优化,lambda表达式参数可以直接是auto:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;


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

结果

3 变量模板

C++14支持变量模板:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

template<class T>
constexpr T pi = T(3.1415926535897932385L);

int main() {
    cout << pi<int> << endl; // 3
    cout << pi<double> << endl; // 3.14159
    return 0;
}

结果

C++11是个警告

4 别名模板

C++14也支持别名模板:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

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;
}

结果

11和14都支持。

5 constexpr的限制

C++14相较于C++11对constexpr减少了一些限制:

(1)C++11中constexpr函数可以使用递归,在C++14中可以使用局部变量和循环

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

在C++14中可以这样做:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

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


int main() {
    return 0;
}

结果

(2)C++11中constexpr函数必须必须把所有东西都放在一个单独的return语句中,而constexpr则无此限制:

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

在C++14中可以这样:

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

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


int main() {
    return 0;
}

结果

6 [[deprecated]]标记

C++14中增加了deprecated标记,修饰类、变、函数等,当程序中使用到了被其修饰的代码时,编译时被产生警告,用户提示开发者该标记修饰的内容将来可能会被丢弃,尽量不要使用。

/*************************************************************************
 > File Name: test01.cpp
 > Author: Winter
 > Created Time: 2024年04月01日 星期一 21时05分37秒
 ************************************************************************/
#include <iostream>
using namespace std;

struct [[deprecated]] A { };


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

结果

11和14都支持。

转载:C++11、C++14、C++17、C++20新特性总结(5万字详解)_c++11 14 17 20区别-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值