From 71c24af74f8fa4ce2e0a160f6209ed3222e17e67 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Fri, 31 Jan 2020 23:16:05 -0500 Subject: [PATCH] C++20 features: new attributes, deprecate implicit capture of this, and classes in non-type template parameters. --- CPP17.md | 4 ++-- CPP20.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/CPP17.md b/CPP17.md index dcb1275..8a483a6 100644 --- a/CPP17.md +++ b/CPP17.md @@ -17,7 +17,7 @@ C++17 includes the following new language features: - [constexpr if](#constexpr-if) - [utf-8 character literals](#utf-8-character-literals) - [direct-list-initialization of enums](#direct-list-initialization-of-enums) -- [new standard attributes](#new-standard-attributes) +- [fallthrough, nodiscard, maybe_unused attributes](#fallthrough-nodiscard-maybe_unused-attributes) C++17 includes the following new library features: - [std::variant](#stdvariant) @@ -260,7 +260,7 @@ byte d = byte{1}; // OK byte e = byte{256}; // ERROR ``` -### New standard attributes +### fallthrough, nodiscard, maybe_unused attributes C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`. * `[[fallthrough]]` indicates to the compiler that falling through in a switch statement is intended behavior. ```c++ diff --git a/CPP20.md b/CPP20.md index a1dae93..629e1fa 100644 --- a/CPP20.md +++ b/CPP20.md @@ -8,6 +8,9 @@ C++20 includes the following new language features: - [designated initializers](#designated-initializers) - [template syntax for lambdas](#template-syntax-for-lambdas) - [range-based for loop with initializer](#range-based-for-loop-with-initializer) +- [likely and unlikely attributes](#likely-and-unlikely-attributes) +- [deprecate implicit capture of this](#deprecate-implicit-capture-of-this) +- [class types in non-type template parameters](#class-types-in-non-type-template-parameters) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -195,6 +198,53 @@ for (auto v = std::vector{1, 2, 3}; auto& e : v) { // prints "123" ``` +### likely and unlikely attributes +Provides a hint to the optimizer that the labelled statement is likely/unlikely to have its body executed. +```c++ +int random = get_random_number_between_x_and_y(0, 3); +[[likely]] if (random > 0) { + // body of if statement + // ... +} + +[[unlikely]] while (unlikely_truthy_condition) { + // body of while statement + // ... +} +``` + +### Deprecate implicit capture of this +Implicitly capturing `this` in a lamdba capture using `[=]` is now deprecated; prefer capturing explicitly using `[=, this]` or `[=, *this]`. +```c++ +struct int_value { + int n = 0; + auto getter_fn() { + // BAD: + // return [=]() { return n; }; + + // GOOD: + return [=, *this]() { return n; }; + } +}; +``` + +### Class types in non-type template parameters +Classes can now be used in non-type template parameters. Objects passed in as template arguments have the type `const T`, where `T` is the type of the object, and has static storage duration. +```c++ +struct foo { + foo() = default; + constexpr foo(int) {} +}; + +template +auto get_foo() { + return f; +} + +get_foo(); // uses implicit constructor +get_foo(); +``` + ## C++20 Library Features ### Concepts library diff --git a/README.md b/README.md index 5ab27c0..44b4fe6 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ C++20 includes the following new language features: - [designated initializers](#designated-initializers) - [template syntax for lambdas](#template-syntax-for-lambdas) - [range-based for loop with initializer](#range-based-for-loop-with-initializer) +- [likely and unlikely attributes](#likely-and-unlikely-attributes) +- [deprecate implicit capture of this](#deprecate-implicit-capture-of-this) +- [class types in non-type template parameters](#class-types-in-non-type-template-parameters) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -26,7 +29,7 @@ C++17 includes the following new language features: - [constexpr if](#constexpr-if) - [utf-8 character literals](#utf-8-character-literals) - [direct-list-initialization of enums](#direct-list-initialization-of-enums) -- [new standard attributes](#new-standard-attributes) +- [fallthrough, nodiscard, maybe_unused attributes](#fallthrough-nodiscard-maybe_unused-attributes) C++17 includes the following new library features: - [std::variant](#stdvariant) @@ -287,6 +290,53 @@ for (std::vector v{1, 2, 3}; auto& e : v) { // prints "123" ``` +### likely and unlikely attributes +Provides a hint to the optimizer that the labelled statement is likely/unlikely to have its body executed. +```c++ +int random = get_random_number_between_x_and_y(0, 3); +[[likely]] if (random > 0) { + // body of if statement + // ... +} + +[[unlikely]] while (unlikely_truthy_condition) { + // body of while statement + // ... +} +``` + +### Deprecate implicit capture of this +Implicitly capturing `this` in a lamdba capture using `[=]` is now deprecated; prefer capturing explicitly using `[=, this]` or `[=, *this]`. +```c++ +struct int_value { + int n = 0; + auto getter_fn() { + // BAD: + // return [=]() { return n; }; + + // GOOD: + return [=, *this]() { return n; }; + } +}; +``` + +### Class types in non-type template parameters +Classes can now be used in non-type template parameters. Objects passed in as template arguments have the type `const T`, where `T` is the type of the object, and has static storage duration. +```c++ +struct foo { + foo() = default; + constexpr foo(int) {} +}; + +template +auto get_foo() { + return f; +} + +get_foo(); // uses implicit constructor +get_foo(); +``` + ## C++20 Library Features ### Concepts library @@ -545,7 +595,7 @@ byte d = byte{1}; // OK byte e = byte{256}; // ERROR ``` -### New standard attributes +### fallthrough, nodiscard, maybe_unused attributes C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`. * `[[fallthrough]]` indicates to the compiler that falling through in a switch statement is intended behavior. ```c++