Add std::clamp

This commit is contained in:
Anthony Calandra
2023-02-18 21:37:51 -05:00
parent 698ac6d5ff
commit 560e1b4390
2 changed files with 24 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ C++17 includes the following new library features:
- [splicing for maps and sets](#splicing-for-maps-and-sets)
- [parallel algorithms](#parallel-algorithms)
- [std::sample](#stdsample)
- [std::clamp](#stdclamp)
C++14 includes the following new language features:
- [binary literals](#binary-literals)
@@ -1200,6 +1201,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
std::cout << guid; // e.g. G1fW2
```
### std::clamp
Clamp given value between a lower and upper bound.
```c++
std::clamp(42, -1, 1); // == 1
std::clamp(-42, -1, 1); // == -1
std::clamp(0, -1, 1); // == 0
// `std::clamp` also accepts a custom comparator:
std::clamp(0, -1, 1, std::less<>{}); // == 0
```
## C++14 Language Features
### Binary literals