Add section about C++17 attributes (#50)

* Add section about C++17 attributes

Explains, in a single piece of code, about [[nodiscard]], [[fallthrough]] and [[maybe_unused]]
This commit is contained in:
Tulio Leao
2019-06-19 12:23:45 -03:00
committed by Anthony Calandra
parent 9f21a120f0
commit 23e7b35c39
2 changed files with 41 additions and 0 deletions

View File

@@ -17,6 +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)
C++17 includes the following new library features:
- [std::variant](#stdvariant)
@@ -237,6 +238,25 @@ byte d = byte{1}; // OK
byte e = byte{256}; // ERROR
```
### New standard attributes
C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`:
```c++
// Will warn if return of foo() is ignored
[[nodiscard]] int foo();
int main() {
int a {1};
switch (a) {
// Indicates that falling through on case 1 is intentional
case 1: [[fallthrough]]
case 2:
// Indicates that b might be unused, such as on production builds
[[maybe_unused]] int b = foo();
assert(b > 0);
break;
}
}
```
## C++17 Library Features
### std::variant

View File

@@ -23,6 +23,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)
C++17 includes the following new library features:
- [std::variant](#stdvariant)
@@ -454,6 +455,26 @@ byte d = byte{1}; // OK
byte e = byte{256}; // ERROR
```
### New standard attributes
C++17 introduces three new attributes: `[[fallthrough]]`, `[[nodiscard]]` and `[[maybe_unused]]`:
```c++
// Will warn if return of foo() is ignored
[[nodiscard]] int foo();
int main() {
int a {1};
switch (a) {
// Indicates that falling through on case 1 is intentional
case 1: [[fallthrough]]
case 2:
// Indicates that b might be unused, such as on production builds
[[maybe_unused]] int b = foo();
assert(b > 0);
break;
}
}
```
## C++17 Library Features
### std::variant