Consider the following code:
int main()
{
std::vector<std::chrono::steady_clock::time_point> time;
time.push_back(std::chrono::steady_clock::now());
std::this_thread::sleep_for(std::chrono::milliseconds(4));
time.push_back(std::chrono::steady_clock::now());
std::this_thread::sleep_for(std::chrono::milliseconds(7));
time.push_back(std::chrono::steady_clock::now());
std::vector<std::chrono::duration<double>> diffs;
std::adjacent_difference(time.begin(),time.end(),std::back_inserter(diffs));
}
It does not compile(ugly template error message about mismatched types).
When I try to switch to type in the error message(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1, 1000000000>>>) error message moves around.
My assumption is that algorithm does not work because result of substracting 2 timepoints is not a timepoint, i.e. this lines from pseudocode are conflicing.
template<class InputIt, class OutputIt>
constexpr // since C++20
OutputIt adjacent_difference(InputIt first, InputIt last,
OutputIt d_first)
{
if (first == last) return d_first;
typedef typename std::iterator_traits<InputIt>::value_type value_t;
value_t acc = *first;
*d_first = acc; // <----------------------------------------------------- 1
while (++first != last) {
value_t val = *first;
*++d_first = val - std::move(acc); // std::move since C++20 <-------- 2
acc = std::move(val);
}
return ++d_first;
}
So I have 2 questions:
- Is my guess correct?
- What is the simplest fix? Best I can think of is ugly transform from timepoint to duration as a middlestep.
Although chrono is C++11 I am tagging this C++20 since I am open to any C++20 solutions, although I prefer them to not be ranges since they are not implemented in my compiler.