Skip to content

Commit 560e1b4

Browse files
Add std::clamp
1 parent 698ac6d commit 560e1b4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CPP17.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ C++17 includes the following new library features:
3232
- [splicing for maps and sets](#splicing-for-maps-and-sets)
3333
- [parallel algorithms](#parallel-algorithms)
3434
- [std::sample](#stdsample)
35+
- [std::clamp](#stdclamp)
3536

3637
## C++17 Language Features
3738

@@ -536,6 +537,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
536537
std::cout << guid; // e.g. G1fW2
537538
```
538539
540+
### std::clamp
541+
Clamp given value between a lower and upper bound.
542+
```c++
543+
std::clamp(42, -1, 1); // == 1
544+
std::clamp(-42, -1, 1); // == -1
545+
std::clamp(0, -1, 1); // == 0
546+
547+
// `std::clamp` also accepts a custom comparator:
548+
std::clamp(0, -1, 1, std::less<>{}); // == 0
549+
```
550+
539551
## Acknowledgements
540552
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
541553
* [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.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ C++17 includes the following new library features:
6262
- [splicing for maps and sets](#splicing-for-maps-and-sets)
6363
- [parallel algorithms](#parallel-algorithms)
6464
- [std::sample](#stdsample)
65+
- [std::clamp](#stdclamp)
6566

6667
C++14 includes the following new language features:
6768
- [binary literals](#binary-literals)
@@ -1200,6 +1201,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
12001201
std::cout << guid; // e.g. G1fW2
12011202
```
12021203
1204+
### std::clamp
1205+
Clamp given value between a lower and upper bound.
1206+
```c++
1207+
std::clamp(42, -1, 1); // == 1
1208+
std::clamp(-42, -1, 1); // == -1
1209+
std::clamp(0, -1, 1); // == 0
1210+
1211+
// `std::clamp` also accepts a custom comparator:
1212+
std::clamp(0, -1, 1, std::less<>{}); // == 0
1213+
```
1214+
12031215
## C++14 Language Features
12041216

12051217
### Binary literals

0 commit comments

Comments
 (0)