C++ 新版本特性 - C++14
2022/6/3大约 2 分钟
C++ 新版本特性 - C++14
参考资料
特性概览

变量模板
在C++14中:
- lambda表达式参数可以为auto类型,类似于函数模板;
- 可以对捕获列表的捕获变量“赋值”。
详情
#include <iostream>
using namespace std;
template <class T>
constexpr T pi = T(3.1415926535897932385L); // 变量模板
template <class T>
T circular_area(T r)
{ // 函数模板
return pi<T> * r * r; // pi<T> 是变量模板实例化
}
int main()
{
cout << circular_area(2) << endl; // 12
cout << circular_area(2.0) << endl; // 12.5664
return 0;
}别名模板
详情
#include <iostream>
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.5;
b.u = 20.8;
std::cout << b.t << ", " << b.u << std::endl; //10.5, 20
return 0;
}泛型lambda和lambda初始化捕获
详情
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 2;
// 捕获列表的捕获变量“赋值”
[a = sin(a)]()
{
std::cout << a << std::endl; // 0.909297
std::cout << cos(a) << std::endl; // 0.6143
}();
std::cout << a << std::endl; // 2
std::cout << cos(a) << std::endl; // -0.416147
// lambda表达式参数可以为auto类型
auto f = [](auto a)
{ return a; };
std::cout << f(1) << std::endl; // 1
std::cout << f(1.1) << std::endl; // 1.1
return 0;
}放松对constexpr函数的限制
C++11中的常量表达式函数:
- 函数体只有单一的return返回语句;
- 函数必须有返回值(不能是void函数)
- 在使用前必须有定义
- return返回语句表达式中不能使用非常量表达式的函数,全局数据,且必须是一个常量表达式
详情
#include <cmath>
#include <iostream>
using namespace std;
constexpr int factorial(int n) { // 在C++11或者C++14中均可以编译通过
return n <= 1 ? 1 : (n * factorial(n - 1));
}
constexpr int factorial_c14(int n) { // 只有在C++14中可以编译通过
int ret = 1;
for (int i = 1; i <= n; ++i) {
ret *= i;
}
return ret;
}
int main()
{
std::cout << factorial(5) << std::endl; // 120
std::cout << factorial_c14(5) << std::endl; // 在c++11下,会报error: body of ‘constexpr’ function ‘constexpr int factorial_c14(int)’ not a return-statement
return 0;
}deprecated标记
C++14中增加了[[deprecated]]标记,可以修饰类、函数、变量等,当程序中使用了被其修饰的模块时,编译器会产生告警,提示用户该标记标记的内容将来可能会被废弃,尽量不要使用。
详情
#include <cmath>
#include <iostream>
using namespace std;
void [[deprecated]] fun(){};
int main()
{
/*
fun.cpp:6:6: warning: attribute ignored [-Wattributes]
6 | void [[deprecated]] fun(){};
| ^
fun.cpp:6:6: note: an attribute that appertains to a type-specifier is ignored
*/
fun();
return 0;
}二进制字面量和数位分隔符
详情
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 0b0001'1111'1010;
double b = 3.14'1592'6535'8979;
std::cout << "a:" << a << std::endl; // 506
std::cout << "b:" << b << std::endl; // 3.14159
return 0;
}库相关
- std::make_unique
- C++11中没有std::make_unique,在C++14中实现了这个方法
- std
shared_lock - C++14中通过std
shared_lock实现读写锁,保证多个线程可以同时读,但是写线程必须独立运行,写操作不能和读操作同时进行。
- C++14中通过std
- std::integer_sequence
- 表示一个编译时的整数序列。在用作函数模板的实参时,能推导参数包Ints并将它用于包展开。
- std::exchange
- 使用new_value 替换 obj 的值,并返回 obj 的旧值。(右边替换左边,返回左边的初值);T 必须满足可移动构造 (MoveConstructible) 的要求。而且必须能移动赋值 U 类型对象给 T 类型对象。
详情
#include <utility>
template <class T, class U = T>
T exchange(T &obj, U &&new_value)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}- std::quoted
- 用于给字符串添加双引号
详情
#include <iostream>
#include <mutex>
#include <memory>
#include <thread>
#include <shared_mutex>
#include <array>
#include <tuple>
#include <utility>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <string>
#include <iterator>
struct A
{
};
void make_unique_fun()
{
std::cout << "make_unique_fun" << std::endl;
std::unique_ptr<A> ptr = std::make_unique<A>();
}
class ThreadSafe
{
public:
ThreadSafe() { _value = 0; }
int get() const
{
std::shared_lock<std::shared_timed_mutex> loc(_mutex);
return _value;
}
void increase()
{
std::unique_lock<std::shared_timed_mutex> lock(_mutex);
_value += 1;
}
private:
mutable std::shared_timed_mutex _mutex;
int _value;
};
void threadSafe_fun()
{
std::cout << "threadSafe_fun" << std::endl;
auto thread = new ThreadSafe();
thread->increase();
thread->increase();
std::cout << "thread->get:" << thread->get() << std::endl;
}
template <typename T, T... ints>
void print_sequence(std::integer_sequence<T, ints...> int_seq)
{
std::cout << "The sequence of size " << int_seq.size() << ": ";
((std::cout << ints << ' '), ...);
std::cout << '\n';
}
// 转换数组为 tuple
template <typename Array, std::size_t... I>
auto a2t_impl(const Array &a, std::index_sequence<I...>)
{
return std::make_tuple(a[I]...);
}
template <typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
auto a2t(const std::array<T, N> &a)
{
return a2t_impl(a, Indices{});
}
// 漂亮地打印 tuple
template <class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple_impl(std::basic_ostream<Ch, Tr> &os, const Tuple &t, std::index_sequence<Is...>)
{
((os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), ...);
}
template <class Ch, class Tr, class... Args>
auto &operator<<(std::basic_ostream<Ch, Tr> &os, const std::tuple<Args...> &t)
{
os << "(";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << ")";
}
void integer_sequence_fun()
{
std::cout << "print_sequence_fun" << std::endl;
print_sequence(std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{});
print_sequence(std::make_integer_sequence<int, 20>{});
print_sequence(std::make_index_sequence<10>{});
print_sequence(std::index_sequence_for<float, std::iostream, char>{});
std::array<int, 4> array = {1, 2, 3, 4};
// 转换 array 为 tuple
auto tuple = a2t(array);
static_assert(std::is_same<decltype(tuple), std::tuple<int, int, int, int>>::value, "");
// 打印到 cout
std::cout << tuple << '\n';
}
void exchange_fun()
{
std::cout << "exchange_fun" << std::endl;
std::vector<int> vec1{1, 2};
std::vector<int> vec2{3, 4};
std::cout << "exchange before: " << std::endl;
std::cout << "vec1: " << std::endl;
std::copy(vec1.begin(), vec1.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << std::endl;
std::cout << "vec2: " << std::endl;
std::copy(vec2.begin(), vec2.end(), std::ostream_iterator<int>{std::cout, " "});
exchange(vec1, vec2);
std::cout << std::endl;
std::cout << "exchange after: " << std::endl;
std::cout << "vec1: " << std::endl;
copy(vec1.begin(), vec1.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << std::endl;
std::cout << "vec2: " << std::endl;
copy(vec2.begin(), vec2.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << std::endl;
}
void quoted_fun()
{
std::cout << "quoted_fun" << std::endl;
std::string str{"hello world"};
std::cout << "origin Str:" << str << std::endl; // hello world
std::cout << "quoted Str:" << std::quoted(str) << std::endl; // "hello world"
}
int main()
{
make_unique_fun();
threadSafe_fun();
integer_sequence_fun();
exchange_fun();
quoted_fun();
return 0;
}执行结果:
详情
[root@iZuf61kbf845xt6tz10abgZ c14]# ./std_lib
make_unique_fun
threadSafe_fun
thread->get:2
print_sequence_fun
The sequence of size 7: 9 2 5 1 9 1 6
The sequence of size 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The sequence of size 10: 0 1 2 3 4 5 6 7 8 9
The sequence of size 3: 0 1 2
(1, 2, 3, 4)
exchange_fun
exchange before:
vec1:
1 2
vec2:
3 4
exchange after:
vec1:
3 4
vec2:
3 4
quoted_fun
origin Str:hello world
quoted Str:"hello world"