![](/img/trans.png)
[英]std::chrono::duration::max() doesn't work with thread support library
[英]std::chrono doesn't work if passing a variable
我在std :: chrono中遇到了一個奇怪的錯誤,如果我這樣做:
TimeHandling time(std::chrono::milliseconds(1000 / 125));
time.start();
一切都好。 但如果相反,我將毫秒值放在變量中:
int mpl = 1000 / 125;
TimeHandling time(std::chrono::milliseconds(mpl));
time.start();
g ++拋出此錯誤:
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> >)}’
有人知道為什么嗎?
第二個版本使用名為mpl
的參數聲明一個函數
請參閱http://en.wikipedia.org/wiki/Most_vexing_parse和https://stackoverflow.com/tags/most-vexing-parse/info
C ++ 11允許您使用大括號來消除聲明中的初始化:
TimeHandling time{std::chrono::milliseconds(mpl)};
或者,使用大括號進行兩次初始化:
TimeHandling time{std::chrono::milliseconds{mpl}};
這條線
TimeHandling time(std::chrono::milliseconds(mpl));
聲明一個返回TimeHandling
對象的函數,並獲取一個名為mpl
的std::chrono::milliseconds
TimeHandling
std::chrono::milliseconds
參數。
當編譯器面臨將這種語句視為函數聲明或構造函數調用的困境時,它(通過標准)強制將其視為函數聲明。
使用大括號初始化來糾正此問題並消除語句歧義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.