patterncppCritical
What are the new features in C++17?
Viewed 0 times
arethefeatureswhatnew
Problem
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?
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?
Solution
Language features:
Templates and Generic Code
-
Template argument deduction for class templates
-
-
Non-type template arguments fixes
-
-
( Folding + ... + expressions ) and Revisions
-
-
modernizing
Lambda
-
constexpr lambdas
-
Capturing
Attributes
-
[[fallthrough]]
Templates and Generic Code
-
Template argument deduction for class templates
- Like how functions deduce template arguments, now constructors can deduce the template arguments of the class
- http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0
-
template - Represents a value of any (non-type template argument) type.
-
Non-type template arguments fixes
-
templatetypename bob> struct foo {}-
( Folding + ... + expressions ) and Revisions
-
auto x{8}; is an int-
modernizing
using with ... and listsLambda
-
constexpr lambdas
- Lambdas are implicitly constexpr if they qualify
-
Capturing
*this in lambdas[*this]{ std::cout
Attributes
-
[[fallthrough]]
, [[nodiscard]], [[maybe_unused]] attributes
-
[[attributes]] on namespaces and enum { erator[[s]] }
-
using in attributes to avoid having to repeat an attribute namespace.
-
Compilers are now required to ignore non-standard attributes they don't recognize.
- The C++14 wording allowed compilers to reject unknown scoped attributes.
Syntax cleanup
-
Inline variables
- Like inline functions
- Compiler picks where the instance is instantiated
- Deprecate static constexpr redeclaration, now implicitly inline.
-
namespace A::B
-
Simple static_assert(expression); with no string
-
no throw unless throw(), and throw() is noexcept(true).
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
-
Hexadecimal float point literals
-
Dynamic memory allocation for over-aligned data
-
Guaranteed copy elision
- Finally!
- Not in all cases, but distinguishes syntax where you are "just creating something" that was called elision, from "genuine elision".
-
Fixed order-of-evaluation for (some) expressions with some modifications
- Not including function arguments, but function argument evaluation interleaving now banned
- Makes a bunch of broken code work mostly, and makes
.then on future work.
-
Direct list-initialization of enums
-
Forward progress guarantees (FPG) (also, FPGs for parallel algorithms)
- I think this is saying "the implementation may not stall threads forever"?
-
u8'U', u8'T', u8'F', u8'8' character literals (string already existed)
-
"noexcept" in the type system
-
__has_include
- Test if a header file include would be an error
- makes migrating from experimental to std almost seamless
-
Arrays of pointer conversion fixes
-
inherited constructors fixes to some corner cases (see P0136R0 for examples of behavior changes)
-
aggregate initialization with inheritance.
-
std::launder, type punning, etc
Library additions:
Data types
-
std::variant
- Almost-always non-empty last I checked?
- Tagged union type
- {awesome|useful}
-
std::optional
- Maybe holds one of something
- Ridiculously useful
-
std::any
- Holds one of anything (that is copyable)
-
std::string_view
std::string like reference-to-character-array or substring
- Never take a
string const& again. Also can make parsing a bajillion times faster.
"hello world"sv
- constexpr
char_traits
-
std::byte off more than they could chew.
- Neither an integer nor a character, just data
Invoke stuff
-
std::invoke
- Call any callable (function pointer, function, member pointer) with one syntax. From the standard INVOKE concept.
-
std::apply
- Takes a function-like and a tuple, and unpacks the tuple into the call.
-
std::make_from_tuple, std::apply applied to object construction
-
is_invocable, is_invocable_r, invoke_result
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0077r2.html
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0604r0.html
- Deprecates
result_of
is_invocable is "can you call Foo with Args... and get something compatible with R", where R=void is default.
invoke_result is std::result_of_t but apparently less confusing?
File System TS v1
-
[class.path]
-
[class.filesystem.error]
-
[class.file_status]
-
[class.directory_entry]
-
[class.directory_iterator] and [clContext
Stack Overflow Q#38060436, score: 1332
Revisions (0)
No revisions yet.