There is no corresponding function to call std :: vector <std :: tuple> push_back
I have an example program containing 6 time points using high_resolution_clock::now() from the standard chrono header. I take the difference in / in each of them, which leads to 3 differences and cast them as auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count(); to microseconds.
I have another variable called durations, which is assigned as follows: auto durations = std::make_tuple(duration1,duration2,duration3); containing previous time differences.
I need to insert this tuple into a vector, so I entered std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list; However, when using list.push_back(durations); I get an error message:
prog.cpp: In function 'int main()': prog.cpp:36:29: error: no matching function for call to 'std::vector<std::tuple<std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> > > >::push_back(std::tuple<long long int, long long int, long long int>&)' list.push_back(durations); I tried to find here std::chrono::microseconds and other std::chrono::duration stuff here , but failed to fix the problem.
I know this has something to do with my type system negligence, but I cannot find this error. Any help would be appreciated, and here is an ideal link .
#include <iostream> #include <chrono> #include <vector> #include <tuple> using namespace std; using namespace std::chrono; void function() { long long number = 0; for( long long i = 0; i != 2000000; ++i ) { number += 5; } } int main() { high_resolution_clock::time_point t1 = high_resolution_clock::now(); high_resolution_clock::time_point t3 = high_resolution_clock::now(); high_resolution_clock::time_point t5 = high_resolution_clock::now(); function(); high_resolution_clock::time_point t2 = high_resolution_clock::now(); high_resolution_clock::time_point t4 = high_resolution_clock::now(); high_resolution_clock::time_point t6 = high_resolution_clock::now(); auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count(); auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ).count(); auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 ).count(); auto durations = std::make_tuple(duration1,duration2,duration3); std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list; list.push_back(durations); cout << duration1 << " -- "<< duration2 << " -- "<< duration3 << " -- "; return 0; } You created a tuple of 3 integers, and you are trying to add it to a vector of 3 durations.
I take the difference for each of them, which leads to 3 differences and cast them as
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();to microseconds.
Why do you call count() for duration after duration_cast executed to convert to microseconds?
Just save the values as microseconds objects and you can add them to the vector:
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ); auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ); auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 ); The type std::chrono::microseconds::count() not std::chrono::microseconds , it is some type of signed integral.
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count(); //decltype(duration1) is not std::chrono::microseconds Thus, you cannot use the variables duration*n* for a vector that expects a microsecond value.
Easy to fix, just put off your count call until you try to print the contents.
It is simple: do not call count .
std::chrono::microseconds is (in your case) a typedef for the type std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> > . It is also the type that you get from running std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ) .
However, this is not what you assign duration1 . You assign the result of calling the count function of this type. And this returns the number of ticks as a number ( long long int in your standard library case), and not as duration .
You get a type mismatch.
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count() Actually gives you a long long in your case, not std::chrono::microseconds . You can fix this using decltype() and changing
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list; For
std::vector<decltype(durations)> list;