# C++14
NOTE: this is a web “mirror” of Anthony Calandra’s modern-cpp-features shared under MIT License (see at bottom). The only reason I do a copy is I hate reading markdowns from github. I want something simple and plain for my own reference.
# Overview
Many of these descriptions and examples are taken from various resources (see Acknowledgements section) and summarized in my own words.
C++14 includes the following new language features:
- binary literals
- generic lambda expressions
- lambda capture initializers
- return type deduction
- decltype(auto)
- relaxing constraints on constexpr functions
- variable templates
- \[\[deprecated\]] attribute
C++14 includes the following new library features:
# C++14 Language Features
# Binary literals
Binary literals provide a convenient way to represent a base-2 number.
It is possible to separate digits with '
.
|
|
# Generic lambda expressions
C++14 now allows the auto
type-specifier in the parameter list, enabling polymorphic lambdas.
|
|
# Lambda capture initializers
This allows creating lambda captures initialized with 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 when it is invoked).
|
|
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
.
|
|
Using this reference-captures can have different names than the referenced variable.
|
|
# 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.
|
|
|
|
# decltype(auto)
The decltype(auto)
type-specifier also deduces a type like auto
does. However, it deduces return types while keeping their references and cv-qualifiers, while auto
will not.
|
|
|
|
See also: decltype (C++11)
.
# Relaxing constraints on constexpr functions
In C++11, constexpr
function bodies could only contain a very limited set of syntaxes, including (but not limited to): typedef
s, using
s, and a single return
statement. In C++14, the set of allowable syntaxes expands greatly to include the most common syntax such as if
statements, multiple return
s, loops, etc.
|
|
# Variable templates
C++14 allows variables to be templated:
|
|
# [[deprecated]] attribute
C++14 introduces the [[deprecated]]
attribute to indicate that a unit (function, class, etc.) is discouraged and likely yield compilation warnings. If a reason is provided, it will be included in the warnings.
|
|
# C++14 Library Features
# User-defined literals for standard library types
New user-defined literals for standard library types, including new built-in literals for chrono
and basic_string
. These can be constexpr
meaning they can be used at compile-time. Some uses for these literals include compile-time integer parsing, binary literals, and imaginary number literals.
|
|
# Compile-time integer sequences
The class template std::integer_sequence
represents a compile-time sequence of integers. There are a few helpers built on top:
std::make_integer_sequence<T, N>
- creates a sequence of0, ..., N - 1
with typeT
.std::index_sequence_for<T...>
- converts a template parameter pack into an integer sequence.
Convert an array into a tuple:
|
|
# std::make_unique
std::make_unique
is the recommended 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 so:
|
|
The compiler is free to call 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:
|
|
See the section on smart pointers (C++11) for more information on std::unique_ptr
and std::shared_ptr
.
# Acknowledgements
- cppreference - especially useful for finding examples and documentation of new library features.
- C++ Rvalue References Explained - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.
- clang and gcc’s standards support pages. Also included here are the proposals for language/library features that I used to help find a description of, what it’s meant to fix, and some examples.
- Compiler explorer
- Scott Meyers’ Effective Modern C++ - highly recommended book!
- Jason Turner’s C++ Weekly - nice collection of C++-related videos.
- What can I do with a moved-from object?
- What are some uses of decltype(auto)?
- And many more SO posts I’m forgetting…
# COPYING & ATTRIBUTIONS:
Author Anthony Calandra
Content Contributors
See: https://github.com/AnthonyCalandra/modern-cpp-features/graphs/contributors
License: The MIT License (MIT) Copyright (c) 2024 Anthony Calandra
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.