Could someone summarise the new items in C++17? |
Okay. I'm taking the dense summary from
http://en.cppreference.com/w/cpp/language/history and expanding a bit
Language changes:
fold-expressions
http://en.cppreference.com/w/cpp/language/fold
1 2 3 4
|
template<class ...Args>
void printer(Args&&... args) {
(std::cout << ... << args) << '\n';
}
|
template argument deduction for constructors
http://en.cppreference.com/w/cpp/language/class_template_deduction
1 2
|
std::tuple t(4, 3, 2.5); // same as auto t = std::make_tuple(4, 3, 2.5);
auto lck = std::lock_guard(mtx); // deduces to std::lock_guard<std::mutex>
|
auto non-type parameters
1 2 3
|
template<auto n> struct B { /* ... */ };
B<5> b1; // OK: non-type template parameter type is int
B<'a'> b2; // OK: non-type template parameter type is char
|
compile-time if constexpr
Example in action:
http://en.cppreference.com/w/cpp/utility/variant/visit#Example
1 2
|
if constexpr (std::is_same_v<T, int>)
std::cout << "int with value " << arg << '\n';
|
inline variables
inline double global = 2.0; // now can put variables in headers
structured binding
http://en.cppreference.com/w/cpp/language/declarations#Decomposition_declaration
const auto [x, y] = f();
if(init;condition) and switch(init;condition)
1 2 3
|
std::set<string> myset;
if (auto [iter, success] = myset.insert("Hello"); success)
do_something_with(iter);
|
u8-char
u8'a'
simplified nested namespaces
1 2 3 4 5 6
|
// before c++17
namespace a {
namespace b {
namespace c {
// since c++17
namespace a::b::c {
|
noexcept is part of type system
allocation functions with explicit alignment:
new T;
calls
operator new(sizeof(T), std::align_val_t(alignof(T))))
guaranteed copy elision
http://en.cppreference.com/w/cpp/language/copy_elision
__has_include
http://en.cppreference.com/w/cpp/preprocessor/include
lambda capture of *this
[=, *this]{};
constexpr lambda
[]() constexpr { body }
attribute namespaces don't have to repeat
new attributes: [[fallthrough]], [[nodiscard]], [[maybe_unused]]
Removed trigraphs, the
register
keyword, and bool increment
Library additions:
std::variant
http://en.cppreference.com/w/cpp/utility/variant
std::has_unique_object_representations
http://en.cppreference.com/w/cpp/types/has_unique_object_representations
std::launder
http://en.cppreference.com/w/cpp/utility/launder
uninitialized memory tools (destroy_at/destroy/destroy_n, uninitialized_move, uninitialized_value_construct, etc)
http://en.cppreference.com/w/cpp/memory#Uninitialized_storage
map/set extract and map/set merge
http://en.cppreference.com/w/cpp/container/map/extract
http://en.cppreference.com/w/cpp/container/map/mege
std::make_from_tuple
http://en.cppreference.com/w/cpp/utility/make_from_tuple
contiguous iterators, non-member size/empty/data
http://en.cppreference.com/w/cpp/iterator/size
http://en.cppreference.com/w/cpp/iterator/empty
http://en.cppreference.com/w/cpp/iterator/data
map/unordered_map try_emplace and insert_or_assign
http://en.cppreference.com/w/cpp/container/map/try_emplace
http://en.cppreference.com/w/cpp/container/map/insert_or_assign
uncaught_exceptions
http://en.cppreference.com/w/cpp/error/uncaught_exception
variadic lock_guard
http://en.cppreference.com/w/cpp/thread/lock_guard
as_const
http://en.cppreference.com/w/cpp/utility/as_const
conjunction/disjunction/negation
http://en.cppreference.com/w/cpp/types/conjunction
http://en.cppreference.com/w/cpp/types/disjunction
http://en.cppreference.com/w/cpp/types/negation
type trait variables xxx_v
transparent owner_less<>
http://en.cppreference.com/w/cpp/memory/owner_less
rounding functions for duration and time_point
http://en.cppreference.com/w/cpp/chrono/duration#Non-member_functions
http://en.cppreference.com/w/cpp/chrono/time_point#Non-member_functions
not_fn
http://en.cppreference.com/w/cpp/utility/functional/not_fn
weak_from_this
http://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this
is_always_lock_free
http://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free
is_swappable
http://en.cppreference.com/w/cpp/types/is_swappable
clamp
http://en.cppreference.com/w/cpp/algorithm/clamp
3D hypot
http://en.cppreference.com/w/cpp/numeric/math/hypot
cache line interface
http://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size
is_callable
http://en.cppreference.com/w/cpp/types/is_callable
From TS's
the filesystem library (withrelative path support (proximate, relative, weakly_canonical) added)
http://en.cppreference.com/w/cpp/filesystem
the library fundamentals v1 (including optional, any, string_view, polymorphic allocators, searchers, apply)
http://en.cppreference.com/w/cpp/utility/optional
http://en.cppreference.com/w/cpp/utility/any
http://en.cppreference.com/w/cpp/string/basic_string_view
http://en.cppreference.com/w/cpp/memory#Allocators
http://en.cppreference.com/w/cpp/utility/functional#Searchers
http://en.cppreference.com/w/cpp/utility/apply
parallelism v1 (including execution policies, reduce, inclusive_scan, exclusive_scan), but removing exception_list.
http://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t
http://en.cppreference.com/w/cpp/algorithm/reduce
http://en.cppreference.com/w/cpp/algorithm/inclusive_scan
http://en.cppreference.com/w/cpp/algorithm/exclusive_scan
http://en.cppreference.com/w/cpp/algorithm/for_each_n
and every algorithm in <algorithm> and <numeric> gets parallelized overloads
From special math IS: special mathematical functions
http://en.cppreference.com/w/cpp/numeric/special_math
From library fundamentals v2: std::gcd, std::lcm
http://en.cppreference.com/w/cpp/numeric/gcd
http://en.cppreference.com/w/cpp/numeric/lcm
From C11: std::aligned_alloc, std::timespec_get
http://en.cppreference.com/w/cpp/memory/c/aligned_alloc
http://en.cppreference.com/w/cpp/chrono/c/timespec_get
Deprecated std::iterator, std::raw_storage_iterator, get_temporary_buffer, is_literal_type, Removed auto_ptr, the deprecated function objects, the obsolete iostreams aliases.