Skip to content

Commit

Permalink
C++20 features: immediate functions, using enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyCalandra committed Feb 2, 2020
1 parent 1e8b4e1 commit 772980b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
44 changes: 44 additions & 0 deletions CPP20.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ C++20 includes the following new language features:
- [constexpr virtual functions](#constexpr-virtual-functions)
- [explicit(bool)](#explicitbool)
- [char8_t](#char8_t)
- [immediate functions](#immediate-functions)
- [using enum](#using-enum)

C++20 includes the following new library features:
- [concepts library](#concepts-library)
Expand Down Expand Up @@ -291,6 +293,48 @@ Provides a standard type for representing UTF-8 strings.
char8_t utf8_str[] = u8"\u0123";
```

### Immediate functions
Similar to `constexpr` functions, but functions with a `consteval` specifier must produce a constant. These are called `immediate functions`.
```c++
consteval int sqr(int n) {
return n * n;
}

constexpr int r = sqr(100); // OK
int x = 100;
int r2 = sqr(x); // ERROR: the value of 'x' is not usable in a constant expression
// OK if `sqr` were a `constexpr` function
```
### using enum
Bring an enum's members into scope to improve readability. Before:
```c++
enum class rgba_color_channel { red, green, blue, alpha };
std::string_view to_string(rgba_color_channel channel) {
switch (channel) {
case rgba_color_channel::red: return "red";
case rgba_color_channel::green: return "green";
case rgba_color_channel::blue: return "blue";
case rgba_color_channel::alpha: return "alpha";
}
}
```
After:
```c++
enum class rgba_color_channel { red, green, blue, alpha };

std::string_view to_string(rgba_color_channel channel) {
switch (my_channel) {
using enum rgba_color_channel;
case red: return "red";
case green: return "green";
case blue: return "blue";
case alpha: return "alpha";
}
}
```
## C++20 Library Features
### Concepts library
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ C++20 includes the following new language features:
- [constexpr virtual functions](#constexpr-virtual-functions)
- [explicit(bool)](#explicitbool)
- [char8_t](#char8_t)
- [immediate functions](#immediate-functions)
- [using enum](#using-enum)

C++20 includes the following new library features:
- [concepts library](#concepts-library)
Expand Down Expand Up @@ -383,6 +385,48 @@ Provides a standard type for representing UTF-8 strings.
char8_t utf8_str[] = u8"\u0123";
```

### Immediate functions
Similar to `constexpr` functions, but functions with a `consteval` specifier must produce a constant. These are called `immediate functions`.
```c++
consteval int sqr(int n) {
return n * n;
}

constexpr int r = sqr(100); // OK
int x = 100;
int r2 = sqr(x); // ERROR: the value of 'x' is not usable in a constant expression
// OK if `sqr` were a `constexpr` function
```
### using enum
Bring an enum's members into scope to improve readability. Before:
```c++
enum class rgba_color_channel { red, green, blue, alpha };
std::string_view to_string(rgba_color_channel channel) {
switch (channel) {
case rgba_color_channel::red: return "red";
case rgba_color_channel::green: return "green";
case rgba_color_channel::blue: return "blue";
case rgba_color_channel::alpha: return "alpha";
}
}
```
After:
```c++
enum class rgba_color_channel { red, green, blue, alpha };

std::string_view to_string(rgba_color_channel channel) {
switch (my_channel) {
using enum rgba_color_channel;
case red: return "red";
case green: return "green";
case blue: return "blue";
case alpha: return "alpha";
}
}
```
## C++20 Library Features
### Concepts library
Expand Down

0 comments on commit 772980b

Please sign in to comment.