为什么 std::chrono::time_point 不喜欢算术?

Why does std::chrono::time_point doesn't like arithmetics?

我只是想测量时间点之间经过的时间:

#include <iostream>
#include <chrono>

int main(){
//    std::chrono::time_point start1;  // <-- doesn't work
//    std::chrono::time_point end1;    // <-- doesn't work
    auto start1;                       // <-- does work
    auto end1;                         // <-- does work

    start1 = std::chrono::high_resolution_clock::now();
    std::cout<<"Hello, World!"<<std::endl;
    end1 = std::chrono::high_resolution_clock::now();

    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1).count() << std::endl;

    return 0;
}

...我注意到 start1end1 必须用 auto 类型定义。如果我用 std::chrono::time_point 类型明确定义它们,表达式 end1 - start1 会给我“二元运算符 '-' 不能应用于...”。

为什么我可以在 start1end1 上使用算术运算符,如果它们是用 auto 定义的? auto 会自动将它们转换为与算术运算符兼容的东西吗?我认为 auto 应该只是编译器的 shorthand 来查看 std::chrono::high_resolution_clock::now() returns 类型是什么?!

I noticed that start1 and end1 have to be defined with the auto type.

它们不必,但它很有用,尤其是对于复杂的模板化类型。

但是,您不能在同一语句中声明一个 auto 变量而不对其进行初始化,否则它无法从中推断出数据类型。它不能像您尝试的那样从另一个语句中推断出类型。

试试这个:

auto start1 = std::chrono::high_resolution_clock::now();
...
auto end1 = std::chrono::high_resolution_clock::now();

If I explicitly define them with the type std::chrono::time_point the expression end1 - start1 gives me "binary operator '-' can not be applied to...".

std::chrono::time_point 是模板化类型:

template< 
  class Clock, 
  class Duration = typename Clock::duration 
class time_point;

您没有为其模板参数提供任何值。

std::chrono::high_resolution_clock::now()returns一个std::chrono::time_point<std::chrono::high_resolution_clock>。如果不使用 auto,则必须在变量声明中指定完整类型:

std::chrono::time_point<std::chrono::high_resolution_clock> start1 = std::chrono::high_resolution_clock::now();
...
std::chrono::time_point<std::chrono::high_resolution_clock> end1 = std::chrono::high_resolution_clock::now();

或者,如果您想将声明与赋值分开:

std::chrono::time_point<std::chrono::high_resolution_clock> start1;
std::chrono::time_point<std::chrono::high_resolution_clock> end1;

start1 = std::chrono::high_resolution_clock::now();
...
end1 = std::chrono::high_resolution_clock::now();

您可以使用 using 别名稍微简化一下:

using hi_res_clock = std::chrono::high_resolution_clock;
using hi_res_time_point = std::chrono::time_point<std::chrono::high_resolution_clock>;

hi_res_time_point start1 = hi_res_clock::now();
...
hi_res_time_point end1 = hi_res_clock::now();

hi_res_time_point start1;
hi_res_time_point end1;

start1 = hi_res_clock::now();
...
end1 = hi_res_clock::now();

Why can I use arithmetic operators on start1 and end1 if they are defined with auto?

因为它们的类型将完全为编译器所知,并且它将能够解析对 time_point 值进行操作的适当 operator-

Does auto automatically cast them into something that is compatible with arithmetic operators?

auto 只是一个 shorthand,它允许编译器根据在编译时分配给该变量的内容自动推断变量的数据类型。

I thought auto should just be the shorthand for the compiler to look what std::chrono::high_resolution_clock::now() returns type is?!

这正是它的作用。您只是没有正确使用 auto