Questions tagged [c++17]

C++17 is the name of the C++ standard approved in 2017. It builds upon the previous C++14 standard, improving the core language and standard library, and adding a few new language features.

C++17 is the name of the C++ standard that came after C++14. It builds upon the previous standard, improving the core language and standard library and adding a few new language features.

The ISO Standard, International Standard ISO/IEC 14882:2017 Programming Language C++, is available for purchase from the ISO website. All major changes from C++14 to C++17 have been enumerated in P0636r0.

Please tag questions about C++17 with the tag, along with the tag.

New Features

Core language

  • template argument deduction for class templates
  • constexpr if statements
  • structured bindings
  • constexpr lambda expressions
  • non-type template parameters with auto type
  • init-statements for if and switch
  • inline variables
  • nested namespace definition
  • fold expressions
  • typename in a template template parameter
  • removing trigraphs
  • u8 character literals
  • aggregate initialization of classes with base classes
  • lambda capture of *this
  • guaranteed copy elision
  • pack expansions in using-declarations
  • hexadecimal floating-point literals

Library

  • new headers:
    • <any>
    • <optional>
    • <variant>
    • <memory_resource>
    • <string_view>
    • <charconv>
    • <execution>
    • <filesystem>
  • minor changes to preexisting headers

Resources

10680 questions
998
votes
1 answer

What are the new features in C++17?

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…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
684
votes
6 answers

What are Aggregates and PODs and how/why are they special?

This FAQ is about Aggregates and PODs and covers the following material: What are Aggregates? What are PODs (Plain Old Data)? How are they related? How and why are they special? What changes for C++11?
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
541
votes
32 answers

enum to string in modern C++11 / C++14 / C++17 and future C++20

Contrary to all other similar questions, this question is about using the new C++ features. 2008 c Is there a simple way to convert C++ enum to string? 2008 c Easy way to use variables of enum types as string in C? 2008 c++ How to easily map c++…
oHo
  • 51,447
  • 27
  • 165
  • 200
365
votes
5 answers

How exactly is std::string_view faster than const std::string&?

std::string_view has made it to C++17 and it is widely recommended to use it instead of const std::string&. One of the reasons is performance. Can someone explain how exactly std::string_view is/will be faster than const std::string& when used as a…
Patryk
  • 22,602
  • 44
  • 128
  • 244
360
votes
3 answers

What is the purpose of std::launder?

P0137 introduces the function template std::launder and makes many, many changes to the standard in the sections concerning unions, lifetime, and pointers. What is the problem this paper is solving? What are the changes to the language that I have…
Barry
  • 286,269
  • 29
  • 621
  • 977
310
votes
4 answers

std::lock_guard or std::scoped_lock?

C++17 introduced a new lock class called std::scoped_lock. Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it?
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
241
votes
2 answers

What is string_view?

string_view was a proposed feature within the C++ Library Fundamentals TS(N3921) added to C++17 As far as i understand it is a type that represent some kind of string "concept" that is a view of any type of container that could store something…
Drax
  • 12,682
  • 7
  • 45
  • 85
208
votes
3 answers

How do inline variables work?

At the 2016 Oulu ISO C++ Standards meeting, a proposal called Inline Variables was voted into C++17 by the standards committee. In layman's terms, what are inline variables, how do they work and what are they useful for? How should inline variables…
jotik
  • 17,044
  • 13
  • 58
  • 123
196
votes
4 answers

What made i = i++ + 1; legal in C++17?

Before you start yelling undefined behaviour, this is explicitly listed in N4659 (C++17) i = i++ + 1; // the value of i is incremented Yet in N3337 (C++11) i = i++ + 1; // the behavior is undefined What changed? From what I can…
Passer By
  • 19,325
  • 6
  • 49
  • 96
194
votes
6 answers

Does C++11, 14, 17 or 20 introduce a standard constant for pi?

There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard. New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions,…
Amomum
  • 6,217
  • 8
  • 34
  • 62
189
votes
6 answers

How to enable C++17 compiling in Visual Studio?

I want to use C++17 features. How can I switch compiling from C++14 to C++17 in Microsoft Visual Studio? Or is it not available in release versions of VS?
Tudvari
  • 2,715
  • 2
  • 14
  • 33
175
votes
3 answers

polymorphic_allocator: when and why should I use it?

Here is the documentation on cppreference, here is the working draft. I must admit that I didn't understand what's the real purpose of polymorphic_allocator and when/why/how I should use it. As an example, the pmr::vector has the following…
skypjack
  • 49,335
  • 19
  • 95
  • 187
143
votes
4 answers

Lambda implicit capture fails with variable declared from structured binding

With the following code, I get a compile error C2065 'a': undeclared identifier (using visual studio 2017): [] { auto [a, b] = [] {return std::make_tuple(1, 2); }(); auto r = [&] {return a; }(); //error C2065 }(); However, the following…
138
votes
9 answers

Most elegant way to write a one-shot 'if'

Since C++ 17 one can write an if block that will get executed exactly once like this: #include int main() { for (unsigned i = 0; i < 10; ++i) { if (static bool do_once = true; do_once) { // Enter only once …
nada
  • 2,109
  • 2
  • 16
  • 23
131
votes
2 answers

Why is there no support for concatenating std::string and std::string_view?

Since C++17, we have std::string_view, a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use…
s3rvac
  • 9,301
  • 9
  • 46
  • 74
1
2 3
99 100