mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-18 18:44:35 +03:00
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:
committed by
Anthony Calandra
parent
9f21a120f0
commit
23e7b35c39
20
CPP17.md
20
CPP17.md
@@ -17,6 +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)
|
||||||
|
|
||||||
C++17 includes the following new library features:
|
C++17 includes the following new library features:
|
||||||
- [std::variant](#stdvariant)
|
- [std::variant](#stdvariant)
|
||||||
@@ -237,6 +238,25 @@ byte d = byte{1}; // OK
|
|||||||
byte e = byte{256}; // ERROR
|
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
|
## C++17 Library Features
|
||||||
|
|
||||||
### std::variant
|
### std::variant
|
||||||
|
|||||||
21
README.md
21
README.md
@@ -23,6 +23,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)
|
||||||
|
|
||||||
C++17 includes the following new library features:
|
C++17 includes the following new library features:
|
||||||
- [std::variant](#stdvariant)
|
- [std::variant](#stdvariant)
|
||||||
@@ -454,6 +455,26 @@ byte d = byte{1}; // OK
|
|||||||
byte e = byte{256}; // ERROR
|
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
|
## C++17 Library Features
|
||||||
|
|
||||||
### std::variant
|
### std::variant
|
||||||
|
|||||||
Reference in New Issue
Block a user