C++14 New Features and Libraries¶
New Features¶
Binary Literals¶
Binary literals provide a convenient way to represent a number in binary. It is possible to separate digits with '
.
1 2 |
|
Generic Lambda Expressions¶
C++14 now allows the auto
type-specifier in the param list, enabling polymorphic lambdas.
1 2 3 |
|
Lambda Capture Initializers¶
This allows creating lambda captures initialized whith arbitrary expressions. The name given to the captured value does not need to be related to any variables in the enclosing scopes and introduces a new name inside the lambda body. The initializing expression is evaluated when the lambda is created(not invoked).
1 2 3 4 5 6 7 8 9 10 |
|
Because it is now possible to move(or forward) values into a lambda that could previously be only captured by copy or reference we can now capture move-only types in a lambda by value. Note that in the below example the p
in the capture-list of task2
on the left-hand-side of =
is a new variable private to the lambda body and does not refer to the original p
.
1 2 3 4 5 6 |
|
Using this reference-captures can have different names than the referenced variable.
1 2 3 4 5 6 |
|
Return Type Deduction¶
Using an auto
return type in C++14, the compiler will attempt to deduce the type for you. With lambdas, you can now deduce its return type using auto
, which makes returning a deduced reference or rvalue reference possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
decltype(auto)
¶
The decltype(auto)
type-specifier also deduces a type like auto
does. The difference is:
decltype(auto)
keeps the reference and cv-qulifiers whileauto
not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Relaxing Constraints on Constexpr Functions¶
In C++11 constexpr
function bodies can only contain a very limited set of syntaxes, including(but not limited to):
typedef
using
return
In C++14, the set of allowable syntaxes expands greatly to include the most common syntaxes such as if
statements, multiple return
s, loops, etc.
1 2 3 4 5 6 7 8 |
|
Variable Templates¶
C++14 allows variables to be templated:
1 2 3 4 |
|
[[deprecated]]
Attribute¶
C++14 introduces the [[deprecated]]
attribute to indicate that a unit (function, clss, etc) is discouraged and likely yield conpilation warnings. If a reason is provided, it will be included in the warning.
1 2 3 4 |
|
New Libraries¶
User-defined Literals for Standard Library Types¶
New user-defineqd literals for standard library types, include new built-in literals for chrono
and basic_string
. These can be constexpr
meaning that they can be used at compile-time. Some uses for these literals include compile-time integar parsing, binary literals, and imaginary number literals.
1 2 3 4 |
|
Compile-time Integar Sequence¶
The class template std::integar_sequence
represents a compile-time sequence of integar. There are a few helpers built on top:
std::make_integar_sequence<T, N>
: creates a sequence of0, ..., N - 1
with typeT
.std::index_sequence_for<T...>
: converts a template parameter pack into an integar sequence.
1 2 3 4 5 6 7 8 9 |
|
std::make_unique
¶
std::make_unique
is the recommanded way to create instances of std::unique_ptr
s due to the following reasons:
- Avoid having to use the
new
operator. - Prevents code repetition when specifying the underlying type the pointer shall hold.
- Most importantly, it provides exception-safety. Suppose we were calling a function
foo
like:
1 |
|
new T{}
, then function_that_throws()
, and so on... Since we have allocated data on the heap in the first construction of a T
, we have introduced a leak here. With std::make_unique
, we are given exception-safety:
1 |
|