Skip to content

Commit aa4cfc4

Browse files
std::bind_front and uniform container erasure.
1 parent 3f05436 commit aa4cfc4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

CPP20.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ C++20 includes the following new library features:
3434
- [std::bit_cast](#stdbit_cast)
3535
- [std::midpoint](#stdmidpoint)
3636
- [std::to_array](#stdto_array)
37+
- [std::bind_front](#stdbind_front)
38+
- [uniform container erasure](#uniform-container-erasure)
3739

3840
## C++20 Language Features
3941

@@ -611,6 +613,25 @@ int a[] = {1, 2, 3};
611613
std::to_array(a); // returns `std::array<int, 3>`
612614
```
613615

616+
### std::bind_front
617+
Binds the first N arguments (where N is the number of arguments after the given function to `std::bind_front`) to a given free function, lambda, or member function.
618+
```c++
619+
const auto f = [](int a, int b, int c) { return a + b + c; };
620+
const auto g = std::bind_front(f, 1, 1);
621+
g(1); // == 3
622+
```
623+
624+
### Uniform container erasure
625+
Provides `std::erase` and/or `std::erase_if` for a variety of STL containers such as string, list, vector, map, etc.
626+
627+
For erasing by value use `std::erase`, or to specify a predicate when to erase elements use `std::erase_if`. Both functions return the number of erased elements.
628+
629+
```c++
630+
std::vector v{0, 1, 0, 2, 0, 3};
631+
std::erase(v, 0); // v == {1, 2, 3}
632+
std::erase_if(v, [](int n) { return n == 0; }); // v == {1, 2, 3}
633+
```
634+
614635
## Acknowledgements
615636
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
616637
* [C++ Rvalue References Explained](http://web.archive.org/web/20240324121501/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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ C++20 includes the following new library features:
3333
- [std::bit_cast](#stdbit_cast)
3434
- [std::midpoint](#stdmidpoint)
3535
- [std::to_array](#stdto_array)
36+
- [std::bind_front](#stdbind_front)
37+
- [uniform container erasure](#uniform-container-erasure)
3638

3739
C++17 includes the following new language features:
3840
- [template argument deduction for class templates](#template-argument-deduction-for-class-templates)
@@ -715,6 +717,25 @@ int a[] = {1, 2, 3};
715717
std::to_array(a); // returns `std::array<int, 3>`
716718
```
717719

720+
### std::bind_front
721+
Binds the first N arguments (where N is the number of arguments after the given function to `std::bind_front`) to a given free function, lambda, or member function.
722+
```c++
723+
const auto f = [](int a, int b, int c) { return a + b + c; };
724+
const auto g = std::bind_front(f, 1, 1);
725+
g(1); // == 3
726+
```
727+
728+
### Uniform container erasure
729+
Provides `std::erase` and/or `std::erase_if` for a variety of STL containers such as string, list, vector, map, etc.
730+
731+
For erasing by value use `std::erase`, or to specify a predicate when to erase elements use `std::erase_if`. Both functions return the number of erased elements.
732+
733+
```c++
734+
std::vector v{0, 1, 0, 2, 0, 3};
735+
std::erase(v, 0); // v == {1, 2, 3}
736+
std::erase_if(v, [](int n) { return n == 0; }); // v == {1, 2, 3}
737+
```
738+
718739
## C++17 Language Features
719740

720741
### Template argument deduction for class templates

0 commit comments

Comments
 (0)