Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::chrono supertype, function argument passing

Tags:

c++

c++11

chrono

I have

auto now = std::chrono::high_resolution_clock::now();

and i want to pass it to a function using a general type for a time point. I do not want specify the resolution or type of clock used.

I've tried using

void my_function(std::chrono::time_point time_point);

but without success. Since apparently std::chrono::time_point isn't a type.

like image 497
user1433688 Avatar asked Nov 19 '12 11:11

user1433688


1 Answers

The std::chrono::time_point is a templated class, that needs at least a clock template parameter.

Either explicitly set the clock, like

void my_function(std::chrono::time_point<std::chrono::high_resolution_clock> time_point);

Or you can make your function a template itself:

template<typename Clock>
void my_function(std::chrono::time_point<Clock> time_point);

In the last case you actually don't have to specify the template parameter when calling the function, the compiler figures it out for you:

my_function(now);
like image 183
Some programmer dude Avatar answered Nov 16 '22 10:11

Some programmer dude