C++14 Library Extensions
The following are the main additions and improvements to the C++ standard library in C++14. There are also miscellaneous smaller improvements besides those listed here, such as addressing tuples by type (e.g., get<string>(t)
) and being able to omit the type of operator functors (e.g., greater<>(x)
is okay).
Shared locking
See also:
- [N3568] Howard Hinnant: Shared locking in C++, Revision 1. This paper contains more informational material. Note that some features were not approved for C++14.
- [N3659] Howard Hinnant: Shared locking in C++, Revision 2. This paper summarizes what was approved for C++14.
User-defined literals for std::
types
C++11 added user-defined literals, but didn’t use them in the standard library. Now some very useful and popular ones work:
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
Note s
means “string” when used on a string literal, and “seconds” when used on an integer literal, without ambiguity.
See also:
- [N3642] Peter Sommerlad: User-defined Literals for Standard Library Types (part 1 – version 4).
make_unique
This fixes an omission:
auto p1 = make_shared<widget>(); // C++11, type is shared_ptr
auto p2 = make_unique<widget>(); // new in C++14, type is unique_ptr
Unlike make_shared
, using make_unique
doesn’t add any optimization. What it does do is enable us to tell people “don’t write naked new
any more to allocate an object on the heap” (except only perhaps in the internals of low-level data structures).
See also:
- [N3656] Stephan T. Lavavej: make_unique (Revision 1).
Type transformation _t
aliases
As part of the movement of C++ away from “traits” types, C++14 added type aliases to avoid having to spell out typename
and ::type
, and actually make the traits more technically usable because they now work in deduced contexts.
// C++11
... typename remove_reference<T>::type ...
... typename make_unsigned<T>::type ...
// new in C++14
... remove_reference_t<T> ...
... make_unsigned_t<T> ...
See also:
- [N3655] Walter E. Brown: TransformationTraits Redux, v2. Note part 4 was not adopted.