C++14对函数返回类型推导规则做了优化,先看一段代码:
#include using namespace std;auto func(int i) {return i;
}int main() {cout << func(4) << endl;return 0;
}
使用C++11编译:
~/test$ g++ test.cc -std=c++11
test.cc:5:16: error: ‘func’ function uses ‘auto’ type specifier without trailing return typeauto func(int i) {^
test.cc:5:16: note: deduced return type only available with -std=c++14 or -std=gnu++14上面的代码使用C++11是不能通过编译的,通过编译器输出的信息也可以看见这个特性需要到C++14才被支持。返回值类型推导也可以用在模板中:```cpp
#include
using namespace std;template auto func(T t) { return t; }int main() {cout << func(4) << endl;cout << func(3.4) << endl;return 0;
}
注意:
auto func(bool flag) {if (flag) return 1;else return 2.3; // error
}
// inconsistent deduction for auto return type: ‘int’ and then ‘double’
auto func() {return {1, 2, 3}; // error returning initializer list
}
struct A {// error: virtual function cannot have deduced return typevirtual auto func() { return 1; }
}
auto f(); // declared, not yet defined
auto f() { return 42; } // defined, return type is intint main() {cout << f() << endl;
}
auto sum(int i) {if (i == 1)return i; // return intelsereturn sum(i - 1) + i; // ok
}
C++14 增加了 decltype(auto)的语法。这允许不必显式指定作为 decltype 参数的表达式,而使用 decltype 对于给定表达式的推断规则。
decltype(auto)的语法也可以用于返回类型推导,只需用 decltype(auto)代替 auto。
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;
auto lambda = [](auto x, auto y) {return x + y;}
泛型 lambda 函数遵循模板参数推导的规则。以上代码的作用与下面的代码相同:
struct unnamed_lambda
{
template
auto operator()(T x, U y) const {return x + y;}
};auto lambda = unnamed_lambda();
C++11 的 lambda 函数通过值拷贝(by copy)或引用(by reference)捕获(capture)已在外层作用域声明的变量。这意味着 lambda 的值成员不可以是 move-only 的类型。C++14允许被捕获的成员用任意的表达式初始化。这既允许了 capture by value-move,也允许了任意声明 lambda 的成员,而不需要外层作用域有一个具有相应名字的变量。
这是通过使用一个初始化表达式完成的:
auto lambda = [value = 1] {return value;}
lambda 函数 lambda 的返回值是 1,说明 value 被初始化为 1。被声明的捕获变量的类型会根据初始化表达式推断,推断方式与用 auto 声明变量相同。
使用标准函数 std::move 可以使之被用以通过 move 捕获:
auto ptr = std::make_unique(10); //See below for std::make_uniqueauto lambda = [ptr = std::move(ptr)] {return *ptr;}
声明 ptr = std::move(ptr)使用了两次 ptr。第一次使用声明了一个新的变量,但在捕获部分,这个变量还不在作用域内。所以第二个 ptr 表示之前在 lambda 之外声明的变量。
C++14支持变量模板:
template
constexpr T pi = T(3.1415926535897932385L);int main() {cout << pi << endl; // 3cout << pi << endl; // 3.14159return 0;
}
C++14也支持别名模板:
template
struct A {T t;U u;
};template
using B = A;int main() {B b;b.t = 10;b.u = 20;cout << b.t << endl;cout << b.u << endl;return 0;
}
C++14相较于C++11对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;
}
constexpr int func(bool flag) { // C++14 和 C++11均可return 0;
}
在C++14中可以这样:
constexpr int func(bool flag) { // C++11中不可,C++14中可以if (flag) return 1;else return 0;
}
C++14中增加了deprecated标记,修饰类、变、函数等,当程序中使用到了被其修饰的代码时,编译时被产生警告,用户提示开发者该标记修饰的内容将来可能会被丢弃,尽量不要使用。
struct [[deprecated]] A { };int main() {A a;return 0;
}
当编译时,会出现如下警告:
~/test$ g++ test.cc -std=c++14
test.cc: In function ‘int main()’:
test.cc:11:7: warning: ‘A’ is deprecated [-Wdeprecated-declarations]A a;^
test.cc:6:23: note: declared herestruct [[deprecated]] A {
C++14引入了二进制字面量,也引入了分隔符,防止看起来眼花哈~
int a = 0b0001'0011'1010;
double b = 3.14'1234'1234'1234;
我们都知道C++11中有std::make_shared,却没有std::make_unique,在C++14已经改善。
struct A {};
std::unique_ptr ptr = std::make_unique();
C++14通过std::shared_timed_mutex和std::shared_lock来实现读写锁,保证多个线程可以同时读,但是写线程必须独立运行,写操作不可以同时和读操作一起进行。
实现方式如下:
struct ThreadSafe {mutable std::shared_timed_mutex mutex_;int value_;ThreadSafe() {value_ = 0;}int get() const {std::shared_lock loc(mutex_);return value_;}void increase() {std::unique_lock lock(mutex_);value_ += 1;}
};
为什么是timed的锁呢,因为可以带超时时间,具体可以自行查询相关资料哈,网上有很多。
std::integer_sequence
template
void print_sequence(std::integer_sequence int_seq)
{std::cout << "The sequence of size " << int_seq.size() << ": ";((std::cout << ints << ' '), ...);std::cout << '\n';
}int main() {print_sequence(std::integer_sequence{});return 0;
}输出:7 9 2 5 1 9 1 6
template
auto map_filter_tuple(F f, T& t) {return std::make_tuple(f(std::get(t))...);
}template
auto map_filter_tuple(std::index_sequence, F f, T& t) {return std::make_tuple(f(std::get(t))...);
}template
auto map_filter_tuple(F&& f, T& t) {return map_filter_tuple(S{}, std::forward(f), t);
}
直接看代码吧:
int main() {std::vector v;std::exchange(v, {1,2,3,4});cout << v.size() << endl;for (int a : v) {cout << a << " ";}return 0;
}
看样子貌似和std::swap作用相同,那它俩有什么区别呢?
可以看下exchange的实现:
template
constexpr T exchange(T& obj, U&& new_value) {T old_value = std::move(obj);obj = std::forward(new_value);return old_value;
}
可以看见new_value的值给了obj,而没有对new_value赋值,这里相信您已经知道了它和swap的区别了吧!
C++14引入std::quoted用于给字符串添加双引号,直接看代码:
int main() {string str = "hello world";cout << str << endl;cout << std::quoted(str) << endl;return 0;
}
编译&输出:
~/test$ g++ test.cc -std=c++14
~/test$ ./a.out
hello world
"hello world"
参考链接:https://blog.csdn.net/qq_41854911/article/details/119657617