C++20 features: new attributes, deprecate implicit capture of this, and classes in non-type template parameters.

This commit is contained in:
Anthony Calandra
2020-01-31 23:16:05 -05:00
parent 2e754375fd
commit 71c24af74f
3 changed files with 104 additions and 4 deletions

View File

@@ -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++

View File

@@ -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 <foo f>
auto get_foo() {
return f;
}
get_foo(); // uses implicit constructor
get_foo<foo{123}>();
```
## C++20 Library Features
### Concepts library

View File

@@ -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 <foo f>
auto get_foo() {
return f;
}
get_foo(); // uses implicit constructor
get_foo<foo{123}>();
```
## 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++