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

@@ -32,6 +32,7 @@ C++17 includes the following new library features:
- [splicing for maps and sets](#splicing-for-maps-and-sets) - [splicing for maps and sets](#splicing-for-maps-and-sets)
- [parallel algorithms](#parallel-algorithms) - [parallel algorithms](#parallel-algorithms)
- [std::sample](#stdsample) - [std::sample](#stdsample)
- [std::clamp](#stdclamp)
## C++17 Language Features ## C++17 Language Features
@@ -536,6 +537,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
std::cout << guid; // e.g. G1fW2 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
```
## Acknowledgements ## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.

View File

@@ -62,6 +62,7 @@ C++17 includes the following new library features:
- [splicing for maps and sets](#splicing-for-maps-and-sets) - [splicing for maps and sets](#splicing-for-maps-and-sets)
- [parallel algorithms](#parallel-algorithms) - [parallel algorithms](#parallel-algorithms)
- [std::sample](#stdsample) - [std::sample](#stdsample)
- [std::clamp](#stdclamp)
C++14 includes the following new language features: C++14 includes the following new language features:
- [binary literals](#binary-literals) - [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::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 ## C++14 Language Features
### Binary literals ### Binary literals