std::bind_front and uniform container erasure.

This commit is contained in:
Anthony Calandra
2024-10-12 22:50:16 -04:00
parent 3f054364ad
commit aa4cfc4422
2 changed files with 42 additions and 0 deletions

View File

@@ -34,6 +34,8 @@ C++20 includes the following new library features:
- [std::bit_cast](#stdbit_cast)
- [std::midpoint](#stdmidpoint)
- [std::to_array](#stdto_array)
- [std::bind_front](#stdbind_front)
- [uniform container erasure](#uniform-container-erasure)
## C++20 Language Features
@@ -611,6 +613,25 @@ int a[] = {1, 2, 3};
std::to_array(a); // returns `std::array<int, 3>`
```
### std::bind_front
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.
```c++
const auto f = [](int a, int b, int c) { return a + b + c; };
const auto g = std::bind_front(f, 1, 1);
g(1); // == 3
```
### Uniform container erasure
Provides `std::erase` and/or `std::erase_if` for a variety of STL containers such as string, list, vector, map, etc.
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.
```c++
std::vector v{0, 1, 0, 2, 0, 3};
std::erase(v, 0); // v == {1, 2, 3}
std::erase_if(v, [](int n) { return n == 0; }); // v == {1, 2, 3}
```
## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
* [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.

View File

@@ -33,6 +33,8 @@ C++20 includes the following new library features:
- [std::bit_cast](#stdbit_cast)
- [std::midpoint](#stdmidpoint)
- [std::to_array](#stdto_array)
- [std::bind_front](#stdbind_front)
- [uniform container erasure](#uniform-container-erasure)
C++17 includes the following new language features:
- [template argument deduction for class templates](#template-argument-deduction-for-class-templates)
@@ -715,6 +717,25 @@ int a[] = {1, 2, 3};
std::to_array(a); // returns `std::array<int, 3>`
```
### std::bind_front
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.
```c++
const auto f = [](int a, int b, int c) { return a + b + c; };
const auto g = std::bind_front(f, 1, 1);
g(1); // == 3
```
### Uniform container erasure
Provides `std::erase` and/or `std::erase_if` for a variety of STL containers such as string, list, vector, map, etc.
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.
```c++
std::vector v{0, 1, 0, 2, 0, 3};
std::erase(v, 0); // v == {1, 2, 3}
std::erase_if(v, [](int n) { return n == 0; }); // v == {1, 2, 3}
```
## C++17 Language Features
### Template argument deduction for class templates