Improve C++17 new attributes section.

This commit is contained in:
Anthony Calandra
2020-01-21 23:14:42 -05:00
parent ce454e0c2d
commit 1c53669d1f
2 changed files with 83 additions and 30 deletions

View File

@@ -17,7 +17,7 @@ C++17 includes the following new language features:
- [constexpr if](#constexpr-if) - [constexpr if](#constexpr-if)
- [utf-8 character literals](#utf-8-character-literals) - [utf-8 character literals](#utf-8-character-literals)
- [direct-list-initialization of enums](#direct-list-initialization-of-enums) - [direct-list-initialization of enums](#direct-list-initialization-of-enums)
- [New standard attributes](#new-standard-attributes) - [new standard attributes](#new-standard-attributes)
C++17 includes the following new library features: C++17 includes the following new library features:
- [std::variant](#stdvariant) - [std::variant](#stdvariant)
@@ -251,21 +251,48 @@ byte e = byte{256}; // ERROR
``` ```
### New standard attributes ### New standard attributes
C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`: 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++ ```c++
// Will warn if return of foo() is ignored switch (n) {
[[nodiscard]] int foo(); case 1: [[fallthrough]]
int main() { // ...
int a {1}; case 2:
switch (a) { // ...
// Indicates that falling through on case 1 is intentional break;
case 1: [[fallthrough]] }
case 2: ```
// Indicates that b might be unused, such as on production builds
[[maybe_unused]] int b = foo(); * `[[nodiscard]]` issues a warning when either a function or class has this attribute and its return value is discarded.
assert(b > 0); ```c++
break; [[nodiscard]] bool do_something() {
} return is_success; // true for success, false for failure
}
do_something(); // warning: ignoring return value of 'bool do_something()',
// declared with attribute 'nodiscard'
```
```c++
// Only issues a warning when `error_info` is returned by value.
struct [[nodiscard]] error_info {
// ...
};
error_info do_something() {
error_info ei;
// ...
return ei;
}
do_something(); // warning: ignoring returned value of type 'error_info',
// declared with attribute 'nodiscard'
```
* `[[maybe_unused]]` indicates to the compiler that a variable or parameter might be unused and is intended.
```c++
void my_callback(std::string msg, [[maybe_unused]] bool error) {
// Don't care if `msg` is an error message, just log it.
log(msg);
} }
``` ```

View File

@@ -26,7 +26,7 @@ C++17 includes the following new language features:
- [constexpr if](#constexpr-if) - [constexpr if](#constexpr-if)
- [utf-8 character literals](#utf-8-character-literals) - [utf-8 character literals](#utf-8-character-literals)
- [direct-list-initialization of enums](#direct-list-initialization-of-enums) - [direct-list-initialization of enums](#direct-list-initialization-of-enums)
- [New standard attributes](#new-standard-attributes) - [new standard attributes](#new-standard-attributes)
C++17 includes the following new library features: C++17 includes the following new library features:
- [std::variant](#stdvariant) - [std::variant](#stdvariant)
@@ -536,22 +536,48 @@ byte e = byte{256}; // ERROR
``` ```
### New standard attributes ### New standard attributes
C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`: 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++ ```c++
// Will warn if return of foo() is ignored switch (n) {
[[nodiscard]] int foo(); case 1: [[fallthrough]]
// ...
case 2:
// ...
break;
}
```
int main() { * `[[nodiscard]]` issues a warning when either a function or class has this attribute and its return value is discarded.
int a {1}; ```c++
switch (a) { [[nodiscard]] bool do_something() {
// Indicates that falling through on case 1 is intentional return is_success; // true for success, false for failure
case 1: [[fallthrough]] }
case 2:
// Indicates that b might be unused, such as on production builds do_something(); // warning: ignoring return value of 'bool do_something()',
[[maybe_unused]] int b = foo(); // declared with attribute 'nodiscard'
assert(b > 0); ```
break; ```c++
} // Only issues a warning when `error_info` is returned by value.
struct [[nodiscard]] error_info {
// ...
};
error_info do_something() {
error_info ei;
// ...
return ei;
}
do_something(); // warning: ignoring returned value of type 'error_info',
// declared with attribute 'nodiscard'
```
* `[[maybe_unused]]` indicates to the compiler that a variable or parameter might be unused and is intended.
```c++
void my_callback(std::string msg, [[maybe_unused]] bool error) {
// Don't care if `msg` is an error message, just log it.
log(msg);
} }
``` ```