美文网首页
C++14常用特性

C++14常用特性

作者: wolfaherd | 来源:发表于2023-01-01 17:30 被阅读0次

需要记忆的(面试六脉神剑)

变量模版
lambda与auto(参数可用auto声明符)
二进制文字
make_unique
shared_timed_mutex
quoted

核心语言特性

  • 变量模版

变量模板定义了一组变量或静态数据成员。

template<class T>
constexpr T pi = T(3.1415926535897932385L); // variable template
 
template<class T>
T circular_area(T r) // function template
{
    return pi<T> * r * r; // pi<T> is a variable template instantiation
}
  • 通用lambda

lambda参数可以使用auto声明符

// generic lambda, operator() is a template with two parameters
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
 
// generic lambda, operator() is a template with one parameter
auto vglambda = [](auto printer)
{
    return [=](auto&&... ts) // generic lambda, ts is a parameter pack
    { 
        printer(std::forward<decltype(ts)>(ts)...);
        // nullary lambda (takes no parameters):
        return [=] { printer(ts...); };
    };
};
 
auto p = vglambda([](auto v1, auto v2, auto v3)
{
    std::cout << v1 << v2 << v3;
});
 
auto q = p(1, 'a', 3.14); // outputs 1a3.14
q();                      // outputs 1a3.14
  • lambda 捕获初始化

允许被捕获的成员可以用任意的表达式初始化。

int x = 4;
 
auto y = [&r = x, x = x + 1]()->int
{
    r += 2;
    return x * x;
}(); // 更新 ::x 到 6 并初始化 y 为 25。
  • constexpr函数上放松限制

在 C++11 的 #08 条中已经提及 constexpr 修饰的函数除了可以包含 using 指令、typedef 语句以及 static_assert 断⾔ 外,只能包含⼀条 return 语句。
⽽ C++14 则放开了该限制,constexpr 修饰的函数可包含 if/switch 等条件语句,也可包含 for 循环。

  • 二进制文字
int b = 0b101010; // C++14
  • 数字分隔符
unsigned long long l1 = 18446744073709550592ull; // C++11
unsigned long long l2 = 18'446'744'073'709'550'592llu; // C++14
unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C++14
unsigned long long l4 = 184467'440737'0'95505'92LLU; // C++14
  • 带默认成员初始化器的聚合类

C++11 增加了默认成员初始化器,如果构造函数没有初始化某个成员,并且这个成员拥有默认成员初始化器,就会⽤默认成员初始化器来初始化成员。
而在 C++11 中,聚合类(aggregate type)的定义被改为「明确排除任何含有默认成员初始化器」的类型。
因此,在 C++11 中,如果⼀个类含有默认成员初始化器,就不允许使⽤聚合初始化。C++14 放松了这⼀限制

标准库特性

  • make_unique

构造一个类型为T的对象,并将其包装在std::unique_ptr中。

unique_ptr<T>(new T(std::forward<Args>(args)...)) // 非数组

unique_ptr<T>(new std::remove_extent_t<T>[size]()) //数组
  • shared_timed_mutex

shared_timed_mutex类是一个同步原语,可用于保护共享数据不被多个线程同时访问。与其他互斥锁类型不同的是,shared_timed_mutex有两个级别的访问:
独占——只有一个线程可以拥有互斥锁。(unique_lock)
共享——多个线程可以共享同一个互斥锁的所有权。(shared_lock)

class R
{
    mutable std::shared_timed_mutex mut;
    /* data */
public:
    R& operator=(const R& other)
    {
        // requires exclusive ownership to write to *this
        std::unique_lock<std::shared_timed_mutex> lhs(mut, std::defer_lock);
        // requires shared ownership to read from other
        std::shared_lock<std::shared_timed_mutex> rhs(other.mut, std::defer_lock);
        std::lock(lhs, rhs);
        /* assign data */
        return *this;
    }
};
  • integer_sequence

类模板std::integer_sequence表示整数的编译时间序列。当用作函数模板的参数时,可以推导出参数pack int,并将其用于包展开。
可以用于生成一个整数序列

  • exchange
template< class T, class U = T >
T exchange( T& obj, U&& new_value );

用new_value替换obj的值,并返回obj的旧值。

  • quoted

允许插入和提取带引号的字符串

#include <iostream>
#include <iomanip>
#include <sstream>
 
void default_delimiter() {
  const std::string in = "std::quoted() quotes this string and embedded \"quotes\" too";
  std::stringstream ss;
  ss << std::quoted(in);
  std::string out;
  ss >> std::quoted(out);
 
  std::cout << "Default delimiter case:\n"
               "read in     [" << in << "]\n"
               "stored as   [" << ss.str() << "]\n"
               "written out [" << out << "]\n\n";
}
 
void custom_delimiter() {
  const char delim {'$'};
  const char escape {'%'};
 
  const std::string in = "std::quoted() quotes this string and embedded $quotes$ $too";
  std::stringstream ss;
  ss << std::quoted(in, delim, escape);
  std::string out;
  ss >> std::quoted(out, delim, escape);
 
  std::cout << "Custom delimiter case:\n"
               "read in     [" << in << "]\n"
               "stored as   [" << ss.str() << "]\n"
               "written out [" << out << "]\n\n";
}
 
int main() {
  default_delimiter();
  custom_delimiter();
}
/*
输出为:
Default delimiter case:
read in     [std::quoted() quotes this string and embedded "quotes" too]
stored as   ["std::quoted() quotes this string and embedded \"quotes\" too"]
written out [std::quoted() quotes this string and embedded "quotes" too]
 
Custom delimiter case:
read in     [std::quoted() quotes this string and embedded $quotes$ $too]
stored as   [$std::quoted() quotes this string and embedded %$quotes%$ %$too$]
written out [std::quoted() quotes this string and embedded $quotes$ $too]
*/

相关文章

  • C++14常用特性

    需要记忆的(面试六脉神剑) 变量模版lambda与auto(参数可用auto声明符)二进制文字make_uniqu...

  • C++14/17新特性

    c++14 函数返回类型auto lambda参数auto lambda捕获参数初始化 [[deprecated]...

  • C++14新特性的所有知识点全在这儿啦!

    前面程序喵介绍过C++11的新特性,在这里(),这篇文章介绍下C++14的新特性。 函数返回值类型推导 C++14...

  • C++17新特性

    程序喵之前已经介绍过C++11的新特性和C++14的新特性,链接如下:xxx,今天向亲爱的读者们介绍下C++17的...

  • envoy启动过程

    C++14 Envoy是使用C++14开发的,先简单了解一下C++14[https://en.wikipedia....

  • Effective.Modern.C++ 中英文版

    想要彻底理解C++11和C++14,不可止步于熟悉它们引入的语言特性(例如,auto型别推导、移动语义、lambd...

  • WKWebView 几个不常用的特性

    WKWebView 几个不常用的特性WKWebView 几个不常用的特性

  • CentOS 7中安装GCC 7

    最新版本的ceph已经使用了c++14新特性,CentOS7中的gcc版本太低,所以需要手动安装新版本的gcc。 ...

  • C++ 11常用特性

    C++ 11常用特性(转)

  • Math方法和es新特性

    es6常用 es7新特性 es8新特性 es9新特性 es10新特性

网友评论

      本文标题:C++14常用特性

      本文链接:https://www.haomeiwen.com/subject/lkkixdtx.html