cpp14 库

C++14 库扩展

以下是 C++14 中 C++ 标准库的主要新增和改进。除了这里列出的,还有其他一些较小的改进,例如通过类型访问元组(例如,get<string>(t))以及可以省略运算符函数对象的类型(例如,greater<>(x) 是允许的)。

共享锁

另请参见

std:: 类型的用户定义字面量

C++11 增加了用户定义字面量,但并未在标准库中使用它们。现在,一些非常有用和流行的字面量已经可以使用了。

auto a_string = "hello there"s;   // type std::string
auto a_minute = 60s;              // type std::chrono::duration = 60 seconds
auto a_day    = 24h;              // type std::chrono::duration = 24 hours

请注意,当用于字符串字面量时,s 表示“string”;当用于整数字面量时,表示“seconds”,两者没有歧义。

另请参见

make_unique

这弥补了一项遗漏。

auto p1 = make_shared<widget>(); // C++11, type is shared_ptr
auto p2 = make_unique<widget>(); // new in C++14, type is unique_ptr

make_shared 不同,使用 make_unique 不会增加任何优化。它的作用是让我们能够告诉人们“不要再为了在堆上分配对象而直接使用裸露的 new”(除非是在低级数据结构的内部)。

另请参见

类型转换 _t 别名

作为 C++ 脱离“traits”类型的一部分,C++14 增加了类型别名,以避免必须写出 typename::type,并且实际上使 traits 更具技术可用性,因为它们现在可以在推导上下文中使用。

// C++11
... typename remove_reference<T>::type ...
... typename make_unsigned<T>::type ...

// new in C++14 
... remove_reference_t<T> ...
... make_unsigned_t<T> ...

另请参见