From ce454e0c2d324e9d07c2ceb3be423a7c4e5d4730 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Sun, 19 Jan 2020 13:48:43 -0500 Subject: [PATCH] Designated initializers; template syntax for lambdas; range-based for loop with initializer. --- CPP20.md | 32 ++++++++++++++++++++++++++++++++ README.md | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/CPP20.md b/CPP20.md index 67d274e..a1dae93 100644 --- a/CPP20.md +++ b/CPP20.md @@ -5,6 +5,9 @@ Many of these descriptions and examples come from various resources (see [Acknow C++20 includes the following new language features: - [concepts](#concepts) +- [designated initializers](#designated-initializers) +- [template syntax for lambdas](#template-syntax-for-lambdas) +- [range-based for loop with initializer](#range-based-for-loop-with-initializer) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -163,6 +166,35 @@ concept C = requires(T x) { ``` See also: [concepts library](#concepts-library). +### Designated initializers +C-style designated initializer syntax. Any member fields that are not explicitly listed in the designated initializer list are default-initialized. +```c++ +struct A { + int x; + int y; + int z = 123; +}; + +A a {.x = 1, .z = 2}; // a.x == 1, a.y == 0, a.z == 2 +``` + +### Template syntax for lambdas +Use familiar template syntax in lambda expressions. +```c++ +auto f = [](std::vector v) { + // ... +}; +``` + +### Range-based for loop with initializer +This feature simplifies common code patterns, helps keep scopes tight, and offers an elegant solution to a common lifetime problem. +```c++ +for (auto v = std::vector{1, 2, 3}; auto& e : v) { + std::cout << e; +} +// prints "123" +``` + ## C++20 Library Features ### Concepts library diff --git a/README.md b/README.md index 596384b..3a361a1 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ Many of these descriptions and examples come from various resources (see [Acknow C++20 includes the following new language features: - [concepts](#concepts) +- [designated initializers](#designated-initializers) +- [template syntax for lambdas](#template-syntax-for-lambdas) +- [range-based for loop with initializer](#range-based-for-loop-with-initializer) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -255,6 +258,35 @@ concept C = requires(T x) { ``` See also: [concepts library](#concepts-library). +### Designated initializers +C-style designated initializer syntax. Any member fields that are not explicitly listed in the designated initializer list are default-initialized. +```c++ +struct A { + int x; + int y; + int z = 123; +}; + +A a {.x = 1, .z = 2}; // a.x == 1, a.y == 0, a.z == 2 +``` + +### Template syntax for lambdas +Use familiar template syntax in lambda expressions. +```c++ +auto f = [](std::vector v) { + // ... +}; +``` + +### Range-based for loop with initializer +This feature simplifies common code patterns, helps keep scopes tight, and offers an elegant solution to a common lifetime problem. +```c++ +for (auto v = std::vector{1, 2, 3}; auto& e : v) { + std::cout << e; +} +// prints "123" +``` + ## C++20 Library Features ### Concepts library @@ -433,8 +465,8 @@ y; // == 0 ``` ```c++ std::unordered_map mapping { - {"a", 1}, - {"b", 2}, + {"a", 1}, + {"b", 2}, {"c", 3}, };