Talk:cpp/chrono/time point
If the example code was for production, I would agree with its use of auto. However, for example code, the auto obscures the concepts that the example is trying to convey. I believe the following code (that only replaces auto) may be a more useful example.
By the way, the comment on the using namespace is perfect.
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
void slow_motion()
{
static int a[] {1,2,3,4,5,6,7,8,9,10,11,12};
while (std::next_permutation(std::begin(a), std::end(a))) { }
}
int main()
{
using namespace std::literals; // enables the usage of 24h instead of std::chrono::hours(24)
std::chrono::time_point<std::chrono::system_clock> now(
std::chrono::system_clock::now()
);
std::time_t t_c = std::chrono::system_clock::to_time_t(now - 24h);
std::cout << "24 hours ago, the time was "
<< std::put_time(std::localtime(&t_c), "%F %T") << '\n';
std::chrono::time_point<std::chrono::steady_clock> start =
std::chrono::steady_clock::now();
slow_motion();
std::chrono::time_point<std::chrono::steady_clock> end
= std::chrono::steady_clock::now();
std::cout
<< "Slow calculations took "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "µs ≈ "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms ≈ "
<< std::chrono::duration_cast<std::chrono::seconds>(end - start).count() << "s.\n";
}
24.246.171.55 03:36, 15 February 2021 (PST) Unferth
- Sounds more than reasonable.) ✔ Done Thank you. --Space Mission (talk) 10:09, 15 February 2021 (PST)
- I think it was bad idea. Old example showed good concise way of using `now`, as it will be used in real word. -- Valiko (talk) 05:55, 16 February 2021 (PST)
- If adding a production code version, perhaps as comments? (130.76.24.18 09:11, 24 February 2021 (PST))
- ✔ Updated, once again.) In Examples the actual types exposure is more useful, IMO. After all, the replacement with
autofor real-code hardly can cause any trouble. --Space Mission (talk) 16:08, 24 February 2021 (PST)- This site is not tutorial on C++, so IMO those long types only distract attention from the essential points. Please also look at the "What is the purpose of this site?" in Cppreference:FAQ -- Valiko (talk) 05:34, 25 February 2021 (PST)
- ✔ Updated, once again.) In Examples the actual types exposure is more useful, IMO. After all, the replacement with
Completely disagree with adding those comments, I think they just add noise to the example, and I don't think anyone needs to be told how to replace a type with auto. Regarding auto type deduction vs explicit types, I think explicit types are more demonstrative, having an example that doesn't even mention the type being documented would be rather embarrassing. I would however encourage the idea of omitting the template arguments (std::chrono::steady_clock) and relying on CTAD, I don't think they're adding any demonstrative value. --Ybab321 (talk) 08:23, 25 February 2021 (PST)
- In the example, one can always click on "std::chrono::system_clock::now" and see what type it returns, no need to explicitly mention that long type. Like iterators (just one more example). -- Valiko (talk) 11:49, 25 February 2021 (PST)
- Whilst it's true that you can click all of the links of functions to see what they return, I think it would be more convenient to the reader to save them the effort. I'm no authority here, but I believe an example should show an enlightening example of how a std::chrono::time_point could be used, not how to a half-assed execution time measurement or how to call std::chrono::steady_clock::now() or any of the other stuff in the example. To that end, I think it makes the most sense for the std::chrono::time_point type to be spelled out and for the reader to know what they're looking at and see what they're looking for at a glance. --Ybab321 (talk) 13:20, 25 February 2021 (PST)
Return value speaks of d, but not defined
For the member functions ceil, floor and round, the return value states:
d rounded to nearest time point using duration of type ToDuration...
yet d is not described. tp is the parameter, and I'd understand if d were a duration. Can someone clarify?