C++14常用特性

2023-01-01  本文已影响0人  wolfaherd

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

变量模版
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参数可以使用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

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

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

在 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 放松了这⼀限制

标准库特性

构造一个类型为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有两个级别的访问:
独占——只有一个线程可以拥有互斥锁。(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;
    }
};

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

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

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

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

#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]
*/
上一篇 下一篇

猜你喜欢

热点阅读