gpt4 book ai didi

c++ - C++ 是如何解析这个返回类型的?

转载 作者:太空狗 更新时间:2023-10-29 23:23:22 26 4
gpt4 key购买 nike

我正在阅读 this article about parallel programming我遇到了一个我不太明白的 return 语句。我读过 namespacesBoost::Chrono::steady_clock ,虽然我从未在实践中使用过它们,但我理解它们的用途。

这是在 run_tests 函数中找到的令我困惑的代码行:

return boost::chrono::duration <double, boost::milli> (end - start).count();

这里究竟发生了什么?对象名称不应该出现在 .count() 之前吗? Chrono 中的 - 运算符是否有一些重载?

The full code can be found here .

最佳答案

       boost::chrono::duration

是类模板的名称。

       boost::chrono::duration <double, boost::milli>

是类模板的实例化,即类。

       boost::chrono::duration <double, boost::milli> (end - start)

创建该类型的临时对象,用表达式 end-start 的值初始化。

       boost::chrono::duration <double, boost::milli> (end - start).count()

调用临时对象的 .count() 方法。

return boost::chrono::duration <double, boost::milli> (end - start).count();

返回 .count() 方法的结果。

关于c++ - C++ 是如何解析这个返回类型的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14717337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com