mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-16 17:47:02 +03:00
Add std::midpoint and std::to_array.
This commit is contained in:
18
CPP20.md
18
CPP20.md
@@ -28,6 +28,8 @@ C++20 includes the following new library features:
|
||||
- [starts_with and ends_with on strings](#starts_with-and-ends_with-on-strings)
|
||||
- [check if associative container has element](#check-if-associative-container-has-element)
|
||||
- [std::bit_cast](#stdbit_cast)
|
||||
- [std::midpoint](#stdmidpoint)
|
||||
- [std::to_array](#stdto_array)
|
||||
|
||||
## C++20 Language Features
|
||||
|
||||
@@ -475,6 +477,22 @@ float f = 123.0;
|
||||
int i = std::bit_cast<int>(f);
|
||||
```
|
||||
|
||||
### std::midpoint
|
||||
Calculate the midpoint of two integers safely (without overflow).
|
||||
```c++
|
||||
std::midpoint(1, 3); // == 2
|
||||
```
|
||||
|
||||
### std::to_array
|
||||
Converts the given array/"array-like" object to a `std::array`.
|
||||
```c++
|
||||
std::to_array("foo"); // returns `std::array<char, 4>`
|
||||
std::to_array<int>({1, 2, 3}); // returns `std::array<int, 3>`
|
||||
|
||||
int a[] = {1, 2, 3};
|
||||
std::to_array(a); // returns `std::array<int, 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://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.
|
||||
|
||||
18
README.md
18
README.md
@@ -28,6 +28,8 @@ C++20 includes the following new library features:
|
||||
- [starts_with and ends_with on strings](#starts_with-and-ends_with-on-strings)
|
||||
- [check if associative container has element](#check-if-associative-container-has-element)
|
||||
- [std::bit_cast](#stdbit_cast)
|
||||
- [std::midpoint](#stdmidpoint)
|
||||
- [std::to_array](#stdto_array)
|
||||
|
||||
C++17 includes the following new language features:
|
||||
- [template argument deduction for class templates](#template-argument-deduction-for-class-templates)
|
||||
@@ -568,6 +570,22 @@ float f = 123.0;
|
||||
int i = std::bit_cast<int>(f);
|
||||
```
|
||||
|
||||
### std::midpoint
|
||||
Calculate the midpoint of two integers safely (without overflow).
|
||||
```c++
|
||||
std::midpoint(1, 3); // == 2
|
||||
```
|
||||
|
||||
### std::to_array
|
||||
Converts the given array/"array-like" object to a `std::array`.
|
||||
```c++
|
||||
std::to_array("foo"); // returns `std::array<char, 4>`
|
||||
std::to_array<int>({1, 2, 3}); // returns `std::array<int, 3>`
|
||||
|
||||
int a[] = {1, 2, 3};
|
||||
std::to_array(a); // returns `std::array<int, 3>`
|
||||
```
|
||||
|
||||
## C++17 Language Features
|
||||
|
||||
### Template argument deduction for class templates
|
||||
|
||||
Reference in New Issue
Block a user