C++如何解决此返回类型?

Joe*_*oey 2 c++ boost namespaces return operator-overloading

我正在读这篇关于并行编程的文章,我发现了一个return我不太了解的声明.我已经阅读过名称空间Boost :: Chrono :: steady_clock,虽然我从来没有在实践中使用,但我理解他们的目的.

这是run_tests函数中找到的代码行,让我感到困惑:

return boost::chrono::duration <double, boost::milli> (end - start).count();
Run Code Online (Sandbox Code Playgroud)

这到底发生了什么?不应该有一个对象名称.count()吗?-在Chrono中是否有一些运算符超载?

完整的代码可以在这里找到.

Rob*_*obᵩ 9

       boost::chrono::duration
Run Code Online (Sandbox Code Playgroud)

是类模板的名称.

       boost::chrono::duration <double, boost::milli>
Run Code Online (Sandbox Code Playgroud)

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

       boost::chrono::duration <double, boost::milli> (end - start)
Run Code Online (Sandbox Code Playgroud)

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

       boost::chrono::duration <double, boost::milli> (end - start).count()
Run Code Online (Sandbox Code Playgroud)

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

return boost::chrono::duration <double, boost::milli> (end - start).count();
Run Code Online (Sandbox Code Playgroud)

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


Jer*_*fin 6

这么多:boost::chrono::duration <double, boost::milli> (end - start)正在构建一个对象,就像int(x)它一样.

然后.count()在该(临时)对象上调用,并从中返回返回的内容.