mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 01:54:36 +03:00
Add rounding functions for chrono durations and timepoints.
This commit is contained in:
12
CPP17.md
12
CPP17.md
@@ -39,6 +39,7 @@ C++17 includes the following new library features:
|
|||||||
- [gcd and lcm](#gcd-and-lcm)
|
- [gcd and lcm](#gcd-and-lcm)
|
||||||
- [std::not_fn](#stdnot_fn)
|
- [std::not_fn](#stdnot_fn)
|
||||||
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
|
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
|
||||||
|
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
|
||||||
|
|
||||||
## C++17 Language Features
|
## C++17 Language Features
|
||||||
|
|
||||||
@@ -686,6 +687,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
|
|||||||
else { /* handle failure */ }
|
else { /* handle failure */ }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rounding functions for chrono durations and timepoints
|
||||||
|
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
|
||||||
|
```c++
|
||||||
|
using seconds = std::chrono::seconds;
|
||||||
|
std::chrono::milliseconds d{ 5500 };
|
||||||
|
std::chrono::abs(d); // == 5s
|
||||||
|
std::chrono::round<seconds>(d); // == 6s
|
||||||
|
std::chrono::ceil<seconds>(d); // == 6s
|
||||||
|
std::chrono::floor<seconds>(d); // == 5s
|
||||||
|
```
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
|
* [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.
|
* [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.
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -70,6 +70,7 @@ C++17 includes the following new library features:
|
|||||||
- [gcd and lcm](#gcd-and-lcm)
|
- [gcd and lcm](#gcd-and-lcm)
|
||||||
- [std::not_fn](#stdnot_fn)
|
- [std::not_fn](#stdnot_fn)
|
||||||
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
|
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
|
||||||
|
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
|
||||||
|
|
||||||
C++14 includes the following new language features:
|
C++14 includes the following new language features:
|
||||||
- [binary literals](#binary-literals)
|
- [binary literals](#binary-literals)
|
||||||
@@ -1360,6 +1361,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
|
|||||||
else { /* handle failure */ }
|
else { /* handle failure */ }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rounding functions for chrono durations and timepoints
|
||||||
|
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
|
||||||
|
```c++
|
||||||
|
using seconds = std::chrono::seconds;
|
||||||
|
std::chrono::milliseconds d{ 5500 };
|
||||||
|
std::chrono::abs(d); // == 5s
|
||||||
|
std::chrono::round<seconds>(d); // == 6s
|
||||||
|
std::chrono::ceil<seconds>(d); // == 6s
|
||||||
|
std::chrono::floor<seconds>(d); // == 5s
|
||||||
|
```
|
||||||
|
|
||||||
## C++14 Language Features
|
## C++14 Language Features
|
||||||
|
|
||||||
### Binary literals
|
### Binary literals
|
||||||
|
|||||||
Reference in New Issue
Block a user