C++17

C++17 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++17 replaced the prior version of the C++ standard, called C++14, and was later replaced by C++20.

History

Before the C++ Standards Committee fixed a 3-year release cycle, C++17's release date was uncertain. In that time period, the C++17 revision was also called C++1z, following C++0x or C++1x for C++11 and C++1y for C++14. The C++17 specification reached the Draft International Standard (DIS) stage in March 2017. This DIS was unanimously approved, with only editorial comments, and the final standard was published as ISO/IEC 14882:2017 in December 2017. Few changes were made to the C++ Standard Template Library, although some algorithms in the <algorithm> header were given support for explicit parallelization and some syntactic enhancements were made.

New features

C++17 introduced many new features. The following lists may be incomplete.

Language

  • Making the text message for static_assert optional
  • Allow typename (as an alternative to class) in a template template parameter
  • New rules for auto deduction from braced-init-list
  • Nested namespace definitions, e.g., namespace X::Y { ... } instead of namespace X { namespace Y { ... } }
  • Allowing attributes for namespaces and enumerators
  • New standard attributes [[fallthrough]], [[maybe_unused]] and [[nodiscard]]
  • UTF-8 (u8) character literals (UTF-8 string literals have existed since C++11; C++17 adds the corresponding character literals for consistency, though as they are restricted to a single byte they can only store "Basic Latin" and C0 control codes, i.e. ASCII)
  • Hexadecimal floating-point literals
  • Use of auto as the type for a non-type template parameter
  • Constant evaluation for all non-type template arguments
  • Fold expressions, for variadic templates
  • A compile-time static if with the form if constexpr (expression)
  • Structured binding declarations, allowing auto [a, b] = getTwoReturnValues();
  • Initializers in if and switch statements
  • copy-initialization and direct-initialization of objects of type T from prvalue expressions of type T (ignoring top-level cv-qualifiers) shall result in no copy or move constructors from the prvalue expression. See copy elision for more information.
  • Some extensions on over-aligned memory allocation
  • Class template argument deduction (CTAD), introducing constructor deduction guides, e.g. allowing std::pair(5.0, false) instead of requiring explicit constructor arguments types std::pair<double, bool>(5.0, false) or an additional helper template function std::make_pair(5.0, false).
  • Inline variables, which allows the definition of variables in header files without violating the one definition rule. The rules are effectively the same as inline functions
  • __has_include, allowing the availability of a header to be checked by preprocessor directives
  • Value of __cplusplus changed to 201703L
  • Exception specifications were made part of the function type
  • Lambda expressions can capture *this by value

Library

  • Most of Library Fundamentals TS I, including:
    • <string_view> header: std::string_view, a read-only non-owning reference to a character sequence or string-slice
    • <optional>: std::optional, for representing optional objects, a data type that may not always be returned by a given algorithm with support for non-return
    • <any>: std::any, for representing the any type, which holds a single value of any type
  • std::uncaught_exceptions, as a replacement of std::uncaught_exception in exception handling. Located in <exception>. uncaught_exceptions counts the number of exceptions in the current thread that have been thrown/rethrown and not yet entered a matching catch clause, whereas uncaught_exception only detects whether stack unwinding is in progress.
  • New insertion functions try_emplace and insert_or_assign for std::map (a tree map) and std::unordered_map (a hash map) key-value associative data structures
  • Uniform container access: std::size, std::empty and std::data
  • Definition of "contiguous iterators"
  • <filesystem>: a file system library, std::filesystem (based on boost::filesystem)
  • Parallel versions of STL algorithms
  • <cmath>: additional mathematical special functions, including elliptic integrals and Bessel functions
  • <variant>: std::variant, a tagged union container
  • A std::byte enum, allowing char to be replaced for data types intending to model a byte of data as a byte rather than a character. Note that this is not a primitive type, does not support direct arithmetic, and is not related to Java/C#'s byte primitive.
  • Logical operator traits: std::conjunction, std::disjunction and std::negation
  • <memory_resource> header, for polymorphic memory resources

Removed features

This revision of C++ not only added new features but also removed a few.

Compiler support

Library support

See also