diff --git a/CPP20.md b/CPP20.md index 7915a9e..324310b 100644 --- a/CPP20.md +++ b/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(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` +std::to_array({1, 2, 3}); // returns `std::array` + +int a[] = {1, 2, 3}; +std::to_array(a); // returns `std::array` +``` + ## 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. diff --git a/README.md b/README.md index affab9c..d44c9c2 100644 --- a/README.md +++ b/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(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` +std::to_array({1, 2, 3}); // returns `std::array` + +int a[] = {1, 2, 3}; +std::to_array(a); // returns `std::array` +``` + ## C++17 Language Features ### Template argument deduction for class templates