Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::chrono doesn't work if passing a variable

Tags:

c++

std

c++11

I came across a weird error with std::chrono, If I do something like this:

TimeHandling time(std::chrono::milliseconds(1000 / 125));
time.start();

Everything is ok. But if instead I put the milliseconds value in a variable:

int mpl = 1000 / 125;
TimeHandling time(std::chrono::milliseconds(mpl));
time.start();

g++ throw this error:

 request for member ‘start’ in ‘time’, which is of non-class type ‘TimeHandling(std::chrono::milliseconds) {aka TimeHandling(std::chrono::duration<long int, std::ratio<1l, 1000l> >)}’

Does someone know why ?

like image 866
izissise Avatar asked Oct 20 '25 23:10

izissise


1 Answers

The second version declares a function with a parameter called mpl

See http://en.wikipedia.org/wiki/Most_vexing_parse and https://stackoverflow.com/tags/most-vexing-parse/info

C++11 allows you to use braces to disambiguate initializations from declarations:

TimeHandling time{std::chrono::milliseconds(mpl)};

or, using braces for both initializations:

TimeHandling time{std::chrono::milliseconds{mpl}};
like image 120
Jonathan Wakely Avatar answered Oct 23 '25 14:10

Jonathan Wakely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!