What are the new features in C++17?

239,109

Language features:

Templates and Generic Code

Lambda

Attributes

Syntax cleanup

Cleaner multi-return and flow control

  • Structured bindings

  • Basically, first-class std::tie with auto

  • Example: * const auto [it, inserted] = map.insert( {"foo", bar} ); * Creates variables it and inserted with deduced type from the pair that map::insert returns.

  • Works with tuple/pair-likes & std::arrays and relatively flat structs

  • Actually named structured bindings in standard

  • if (init; condition) and switch (init; condition)

  • if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)

  • Extends the if(decl) to cases where decl isn't convertible-to-bool sensibly.

  • Generalizing range-based for loops

    • Appears to be mostly support for sentinels, or end iterators that are not the same type as begin iterators, which helps with null-terminated loops and the like.
  • if constexpr

  • Much requested feature to simplify almost-generic code.

Misc

Library additions:

Data types

Invoke stuff

File System TS v1

New algorithms

  • for_each_n

  • reduce

  • transform_reduce

  • exclusive_scan

  • inclusive_scan

  • transform_exclusive_scan

  • transform_inclusive_scan

  • Added for threading purposes, exposed even if you aren't using them threaded

Threading

(parts of) Library Fundamentals TS v1 not covered above or below

Container Improvements

Smart pointer changes

Other std datatype improvements:

Misc

Traits

Deprecated

Isocpp.org has has an independent list of changes since C++14; it has been partly pillaged.

Naturally TS work continues in parallel, so there are some TS that are not-quite-ripe that will have to wait for the next iteration. The target for the next iteration is C++20 as previously planned, not C++19 as some rumors implied. C++1O has been avoided.

Initial list taken from this reddit post and this reddit post, with links added via googling or from the above isocpp.org page.

Additional entries pillaged from SD-6 feature-test list.

clang's feature list and library feature list are next to be pillaged. This doesn't seem to be reliable, as it is C++1z, not C++17.

these slides had some features missing elsewhere.

While "what was removed" was not asked, here is a short list of a few things ((mostly?) previous deprecated) that are removed in C++17 from C++:

Removed:

There were rewordings. I am unsure if these have any impact on code, or if they are just cleanups in the standard:

Papers not yet integrated into above:

  • P0505R0 (constexpr chrono)

  • P0418R2 (atomic tweaks)

  • P0512R0 (template argument deduction tweaks)

  • P0490R0 (structured binding tweaks)

  • P0513R0 (changes to std::hash)

  • P0502R0 (parallel exceptions)

  • P0509R1 (updating restrictions on exception handling)

  • P0012R1 (make exception specifications be part of the type system)

  • P0510R0 (restrictions on variants)

  • P0504R0 (tags for optional/variant/any)

  • P0497R0 (shared ptr tweaks)

  • P0508R0 (structured bindings node handles)

  • P0521R0 (shared pointer use count and unique changes?)

Spec changes:

Further reference:

Share:
239,109
Yakk - Adam Nevraumont
Author by

Yakk - Adam Nevraumont

template<class R, class...Args> auto Y = [](auto f) { auto action = [](auto f, auto&& action)->std::function<R(Args...)> { return [f, action](Args&&...args)mutable ->R{ return f( action(std::ref(f), action), std::forward<Args>(args)... ); }; }; return action(f, action); }; Some fun C++ stuff I've done round here: A: Convert a run-time value or type to a compile-time value, also works with lists of types. A: Named operators, does what it says on the tin A: can_apply, a useful trick that helps make traits like "can serialize" or "can convert to string". A: task, a move-only implementation of std::function. Q: Function curry, that does partial binds and the like. A: Me playing around with hana-like metaprogramming, together with the idea that template work best if everything (including templates) is a type. Here is some evil operator abuse, where I manipulate types with +. A: std::function that returns vectors of its own type. Here is a neat question I didn't ask: Q: Does SFINAE actually work with class templates? the answer seems to be "actually, nope, but nobody noticed". An amusing link: http://stackoverflow.com/reputation which will show your reputation history on stack overflow, if you like that kind of thing. ey eir em

Updated on December 30, 2021

Comments

  • Yakk - Adam Nevraumont
    Yakk - Adam Nevraumont over 2 years

    C++17 is now feature complete, so unlikely to experience large changes. Hundreds of proposals were put forward for C++17.

    Which of those features were added to C++ in C++17?

    When using a C++ compiler that supports "C++1z", which of those features are going to be available when the compiler updates to C++17?

  • L. F.
    L. F. almost 5 years
    Um, memory_order_consume does not seem to be officially deprecated. It is just discouraged in the note. Maybe it makes sense to mention this (with a trailing parenthesis, for example)?