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.[1][2] This DIS was unanimously approved, with only editorial comments,[3] and the final standard was published in December 2017.[4] 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[5] - Allow
typename
(as an alternative toclass
) in a template template parameter[6] - New rules for
auto
deduction from braced-init-list[7][8] - Nested namespace definitions, e.g.,
namespace X::Y { … }
instead ofnamespace X { namespace Y { … } }
[8][9] - Allowing attributes for namespaces and enumerators[10][11]
- New standard attributes
fallthrough
,maybe unused
andnodiscard
[12] - UTF-8 (
u8
) character literals[10][13] (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[14][15]
- Use of
auto
as the type for a non-type template parameter[16] - Constant evaluation for all non-type template arguments[10][17]
- Fold expressions, for variadic templates[10][18]
- A compile-time static
if
with the formif constexpr(expression)
[19] - Structured binding declarations, allowing
auto [a, b] = getTwoReturnValues();
[20] - Initializers in
if
andswitch
statements[21] - copy-initialization and direct-initialization of objects of type
T
from prvalue expressions of typeT
(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[22]
- Class template argument deduction (CTAD), introducing constructor deduction guides, e.g. allowing
std::pair(5.0, false)
instead of requiring explicit constructor arguments typesstd::pair<double, bool>(5.0, false)
or an additional helper template functionstd::make_pair(5.0, false)
.[23][24] - 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[25]- Value of
__cplusplus
changed to201703L
[26] - Exception specifications were made part of the function type[27]
- Lambda expressions can capture "*this" by value[28]
Library
- Most of Library Fundamentals TS I, including:[29][30]
std::string_view
, a read-only non-owning reference to a character sequence or string-slice[31]std::optional
, for representing optional objects, a data type that may not always be returned by a given algorithm with support for non-returnstd::any
, for holding single values of any type
std::uncaught_exceptions
, as a replacement ofstd::uncaught_exception
in exception handling[32][10]- New insertion functions
try_emplace
andinsert_or_assign
forstd::map
andstd::unordered_map
key-value associative data structures[33][34] - Uniform container access:
std::size
,std::empty
andstd::data
[34][35] - Definition of "contiguous iterators"[34][36]
- A file system library based on
boost::filesystem
[37] - Parallel versions of STL algorithms[38]
- Additional mathematical special functions, including elliptic integrals and Bessel functions[39]
std::variant
, a tagged union container[40]std::byte
, allowing char to be replaced for data types intending to model a byte of data as a byte rather than a character[41]- Logical operator traits:
std::conjunction
,std::disjunction
andstd::negation
[42] <memory_resource>
header, for polymorphic memory resources[43]
Removed features
This revision of C++ not only added new features but also removed a few.
- Trigraphs were removed.[44][45]
- Some deprecated types and functions were removed from the standard library, including
std::auto_ptr
,std::random_shuffle
, and old function adaptors.[8][46] These were superseded in C++11 by improved facilities such asstd::unique_ptr
,std::shuffle
,std::bind
, and lambdas. - The (formerly deprecated) use of the keyword
register
as a storage class specifier was removed.[47] This keyword is still reserved but now unused.
Compiler support
- GCC has had complete support for C++17 language features since version 8.[48]
- Clang 5 and later supports all C++17 language features.[49]
- Visual Studio 2017 15.8 (MSVC 19.15) and later supports all C++17 language features.[50][51]
Library support
- libstdc++ since version 9.1 has complete support for C++17 (8.1 without Parallelism TS and referring to C99 instead of C11) [52]
- libc++ as of version 9 has partial support for C++17, with the remainder "in progress" [53]
- Visual Studio 2017 15.8 (MSVC 19.15) Standard Library and later supports all C++17 library features except for "Elementary String Conversions" and referring to C99 instead of C11. "Elementary String Conversions" is added in Visual Studio 2019 16.4[54]
See also
References
- ↑ "N4661 Editors' Report -- Programming Languages -- C++". 21 March 2017. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4661.html.
- ↑ "ISO/IEC DIS 14882: Programming Languages — C++". Archived from the original on 2017-03-25. https://web.archive.org/web/20170325025026/http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4660.pdf.
- ↑ Herb Sutter (6 September 2017). "C++17 is formally approved". https://herbsutter.com/2017/09/06/c17-is-formally-approved/.
- ↑ "ISO/IEC 14882:2017". https://www.iso.org/standard/68564.html.
- ↑ "N3928: Extending static_assert, v2 (Walter E. Brown)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3928.pdf.
- ↑ "N4051: Allow typename in a template template parameter (Richard Smith)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4051.html.
- ↑ "N3922: New Rules for auto deduction from braced-init-list (James Dennett)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3922.html.
- ↑ 8.0 8.1 8.2 "Updates to my trip report". http://isocpp.org/blog/2014/11/updates-to-my-trip-report.
- ↑ "N4230: Nested namespace definition (Robert Kawulak, Andrew Tomazos)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4230.html.
- ↑ 10.0 10.1 10.2 10.3 10.4 "New core language papers adopted for C++17". https://isocpp.org/blog/2014/11/new-papers-adopted-for-cpp17.
- ↑ "N4266: Attributes for namespaces and enumerators (Richard Smith)". http://isocpp.org/files/papers/n4266.html.
- ↑ "N4640: Working Draft, Standard for Programming Language C++". pp. 193–195. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4640.pdf.
- ↑ "N4267: Adding u8 character literals (Richard Smith)". http://isocpp.org/files/papers/n4267.html.
- ↑ Thomas Köppe. "Hexadecimal floating literals for C++". http://wg21.link/p0245r1.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §5.13.4. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "Declaring non-type template parameters with auto". 2016-06-23. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0127r2.html.
- ↑ "N4268: Allow constant evaluation for all non-type template arguments (Richard Smith)". http://isocpp.org/files/papers/n4268.html.
- ↑ "N4295: Folding expressions (Andrew Sutton, Richard Smith)". http://isocpp.org/files/papers/n4295.html.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §9.4.1. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §11.5. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "Selection statements with initializer". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html.
- ↑ "Dynamic memory allocation for over-aligned data". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0035r4.html.
- ↑ "Class template argument deduction". https://en.cppreference.com/w/cpp/language/class_template_argument_deduction.
- ↑ "CppCon 2018: Timur Doumler "Class template argument deduction in C++17"". https://www.youtube.com/watch?v=UDs90b0yjjQ.
- ↑ "N4640: Working Draft, Standard for Programming Language C++". pp. 431–433. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4640.pdf.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §19.8. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "P0012R1: Make exception specifications be part of the type system, version 5". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0012r1.html.
- ↑ "P0018R3: Lambda Capture of *this by Value as [=,*this, version 3"]. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0018r3.html.
- ↑ "Adopt Library Fundamentals V1 TS Components for C++17 (R1)". https://isocpp.org/files/papers/p0220r1.html.
- ↑ "Current Status". https://isocpp.org/std/status.
- ↑ "std::basic_string_view - cppreference.com". http://en.cppreference.com/w/cpp/string/basic_string_view.
- ↑ "N4259: Wording for std::uncaught_exceptions (Herb Sutter)". http://isocpp.org/files/papers/n4259.pdf.
- ↑ "N4279: Improved insertion interface for unique-key maps (Thomas Köppe)". https://isocpp.org/files/papers/n4279.html.
- ↑ 34.0 34.1 34.2 "New standard library papers adopted for C++17". https://isocpp.org/blog/2014/11/new-standard-library-papers-adopted-for-cpp17.
- ↑ "N4280: Non-member size() and more (Riccardo Marcangelo)". https://isocpp.org/files/papers/n4280.pdf.
- ↑ "N4284: Contiguous Iterators (Jens Maurer)". https://isocpp.org/files/papers/n4284.html.
- ↑ "Filesystem Library Proposal (Beman Dawes)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3505.html.
- ↑ "The Parallelism TS Should be Standardized". https://isocpp.org/files/papers/P0024R2.html.
- ↑ "Mathematical Special Functions for C++17, v5". https://isocpp.org/files/papers/P0226R1.pdf.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §23.7. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "A byte type definition". http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0298r3.pdf.
- ↑ "N4659: Working Draft, Standard for Programming Language C++". §23.15.8. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf.
- ↑ "PMR (Polymorphic Memory Resources) fully described -- Nico Josuttis". https://isocpp.org/blog/2018/10/pmr-polymorphic-memory-resources.
- ↑ "N3981: Removing trigraphs??! (Richard Smith)". 2014-05-06. http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3981.html.
- ↑ IBM comment on preparing for a Trigraph-adverse future in C++17 , IBM paper N4210, 2014-10-10. Authors: Michael Wong, Hubert Tong, Rajan Bhakta, Derek Inglis
- ↑ "N4190: Removing auto_ptr, random_shuffle(), And Old <functional> Stuff (Stephan T. Lavavej)". http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm.
- ↑ "C++ Keywords: register". https://en.cppreference.com/w/cpp/keyword/register.
- ↑ "C++ Standards Support in GCC - GNU Project - Free Software Foundation (FSF)". https://gcc.gnu.org/projects/cxx-status.html.
- ↑ "Clang - C++17, C++14, C++11 and C++98 Status". https://clang.llvm.org/cxx_status.html.
- ↑ corob-msft. "Visual C++ Language Conformance". https://docs.microsoft.com/en-us/cpp/visual-cpp-language-conformance.
- ↑ "Announcing: MSVC Conforms to the C++ Standard". 7 May 2018. https://blogs.msdn.microsoft.com/vcblog/2018/05/07/announcing-msvc-conforms-to-the-c-standard/.
- ↑ "Chapter 1. Status". https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html.
- ↑ "libc++ C++17 Status". http://libcxx.llvm.org/cxx1z_status.html.
- ↑ "Announcing: MSVC Conforms to the C++ Standard". 7 May 2018. https://devblogs.microsoft.com/cppblog/announcing-msvc-conforms-to-the-c-standard/.
![]() | Original source: https://en.wikipedia.org/wiki/C++17.
Read more |