C++标准 — C++14特性
一、变量模板
在C++11及之前,我们只有针对类和函数的模板。C++14中,新增了变量模板:
template<class T>
constexpr T pi = T(3.1415926535897932385L);
template<class T>
T circular_area(T r)
{
return pi<T> *r * r;
}
变量模板同样可以在类变量中使用:
template<class T>
class X {
static T s;
};
template<class T>
T X<T>::s = 0;
类似函数和类模板,当变量模板被引用时,则会发生实例化。
二、lambda 表达式的新增功能
1、泛化
支持在 lambda 表达式中使用 auto 定义变量类型:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
auto glambda = [](auto& a) {
cout << a << " "; };
int a[] = {
4, 2, 6, 3, 7, 5 };
for_each(a, a + sizeof(a) / sizeof(int), glambda);
cout << endl;
}