Created
July 21, 2020 08:20
-
-
Save kjk/2cdc964488d0f63ba6b4d6ee1ed1397f to your computer and use it in GitHub Desktop.
Example for https://essential-cpp.programming-books.io/user-defined-literals-d3696e4f87894401af6c2f073d356228 (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <iostream> | |
int main() | |
{ | |
using namespace std::literals::chrono_literals; | |
std::chrono::nanoseconds t1 = 600ns; | |
std::chrono::microseconds t2 = 42us; | |
std::chrono::milliseconds t3 = 51ms; | |
std::chrono::seconds t4 = 61s; | |
std::chrono::minutes t5 = 88min; | |
auto t6 = 2 * 0.5h; | |
auto total = t1 + t2 + t3 + t4 + t5 + t6; | |
std::cout.precision(13); | |
std::cout << total.count() << " nanoseconds" << std::endl; // 8941051042600 nanoseconds | |
std::cout << std::chrono::duration_cast<std::chrono::hours>(total).count() | |
<< " hours" << std::endl; // 2 hours | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment