Last updated on 2024-07-29T22:23:06+08:00
                  
                  
                
              
            
            
              
                
                
     
函数返回值类型推导
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | #include <iostream>
 using namespace std;
 
 auto func(int i) {
 return i;
 }
 
 int main() {
 cout << func(4) << endl;
 return 0;
 }
 
 | 
上面的代码使用 C++11 是不能通过编译的,这个特性需要到 C++14 才被支持
返回值类型推导也可以用在模板中:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | #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;
 }
 
 | 
- 函数内如果有多个 return语句,它们必须返回相同的类型,否则编译失败
- 如果 return语句返回初始化列表,返回值类型推导也会失败
- 如果函数是虚函数,不能使用返回值类型推导
lambda参数 auto
 在 C++11 中,lambda表达式参数需要使用具体的类型声明,在 C++14 中,对此进行优化,lambda表达式参数可以直接是auto
变量模板
C++14 支持变量模板
| 12
 3
 4
 5
 6
 7
 8
 
 | template<class T>constexpr T pi = T(3.1415926535897932385L);
 
 int main() {
 cout << pi<int> << endl;
 cout << pi<double> << endl;
 return 0;
 }
 
 | 
别名模板
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | 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;
 }
 
 | 
constexpr的限制 
C++14 相较于 C++11 对constexpr 减少了一些限制:
- C++11 中 constexpr函数可以使用递归,在 C++14 中可以使用局部变量和循环
- C++11 中 constexpr函数必须必须把所有东西都放在一个单独的 return 语句中,而 C++14 则无此限制:
[[deprecated]]标记
C++14 中增加了 deprecated 标记,修饰类、变、函数等,当程序中使用到了被其修饰的代码时,编译时被产生警告,用户提示开发者该标记修饰的内容将来可能会被丢弃,尽量不要使用
二进制字面量与整形字面量分隔符
C++14 引入了二进制字面量,也引入了分隔符
| 12
 
 | int a = 0b0001'0011'1010;double b = 3.14'1234'1234'1234;
 
 | 
std::make_unique
C++11 中有std::make_shared,却没有std::make_unique,在 C++14 已经改善
std::shared_timed_mutex与 std::shared_lock
C++14 通过std::shared_timed_mutex 和std::shared_lock来实现读写锁,保证多个线程可以同时读,但是写线程必须独立运行,写操作不可以同时和读操作一起进行
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | struct ThreadSafe {mutable std::shared_timed_mutex mutex_;
 int value_;
 
 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;
 }
 };
 
 | 
std::integer_sequence
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 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';
 }
 
 int main() {
 print_sequence(std::integer_sequence<int, 9, 2, 5, 1, 9, 1, 6>{});
 return 0;
 }
 
 输出:7 9 2 5 1 9 1 6
 
 | 
std::exchange
std::exchange实现
| 12
 3
 4
 5
 6
 
 | template<class T, class U = T>constexpr T exchange(T& obj, U&& new_value) {
 T old_value = std::move(obj);
 obj = std::forward<U>(new_value);
 return old_value;
 }
 
 | 
std::quoted
C++14 引入 std::quoted 用于给字符串添加双引号
| 12
 3
 4
 5
 6
 
 | int main() {string str = "hello world";
 cout << str << endl;
 cout << std::quoted(str) << endl;
 return 0;
 }
 
 |