[英]mismatched types 'std::chrono::_V2::steady_clock' and 'std::chrono::_V2::system_clock'
I'm trying to build my program in mingw64 (GCC v11.2).我正在尝试在mingw64 (GCC v11.2) 中构建我的程序。 I have the following struct :
我有以下结构:
In a header file:在 header 文件中:
struct Timer
{
std::chrono::time_point< std::chrono::steady_clock > start;
std::chrono::time_point< std::chrono::steady_clock > end;
Timer( );
~Timer( );
};
In a source file:在源文件中:
util::Timer::Timer( )
: start( std::chrono::high_resolution_clock::now( ) )
{
}
util::Timer::~Timer( )
{
end = std::chrono::high_resolution_clock::now( );
std::chrono::duration< double, std::milli > duration_ms { end - start };
std::clog << "\nTimer took " << duration_ms.count( ) << " ms\n";
}
But this happens:但是会发生这种情况:
error: no matching function for call to 'std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >::time_point(std::chrono::_V2::system_clock::time_point)'
8 | : start( std::chrono::high_resolution_clock::now( ) )
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from pch.h:23:
c:\mingw64\include\c++\11.2.0\chrono:871:21: note: candidate: 'template<class _Dur2, class> constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [with _Dur2 = _Dur2; <template-parameter-2-2> = <template-parameter-1-2>; _Clock = std::chrono::_V2::steady_clock; _Dur = std::chrono::duration<long long int, std::ratio<1, 1000000000> >]'
871 | constexpr time_point(const time_point<clock, _Dur2>& __t)
| ^~~~~~~~~~
c:\mingw64\include\c++\11.2.0\chrono:871:21: note: template argument deduction/substitution failed:
Util.cpp:8:3: note: mismatched types 'std::chrono::_V2::steady_clock' and 'std::chrono::_V2::system_clock'
8 | : start( std::chrono::high_resolution_clock::now( ) )
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why is this happening?为什么会这样? How to fix it?
如何解决?
Thanks to the information provided in the comments, I came up with the following solution:感谢评论中提供的信息,我想出了以下解决方案:
In the header file:在 header 文件中:
struct Timer
{
std::chrono::time_point< std::chrono::steady_clock > start;
std::chrono::time_point< std::chrono::steady_clock > end;
Timer( );
~Timer( );
};
In the source file:在源文件中:
util::Timer::Timer( )
: start( std::chrono::steady_clock::now( ) )
{
}
util::Timer::~Timer( )
{
end = std::chrono::steady_clock::now( );
std::clog << "\nTimer took " << std::chrono::duration< double, std::milli >( end - start ).count( ) << " ms\n";
}
So in short, I switched from std::chrono::high_resolution_clock::now( )
to std::chrono::steady_clock::now( )
because high_resolution_clock
has different implementations on different compilers according to high_resolution_clock .所以简而言之,我从
std::chrono::high_resolution_clock::now( )
chrono::high_resolution_clock::now( ) 切换到std::chrono::steady_clock::now( )
因为high_resolution_clock
根据high_resolution_clock在不同的编译器上有不同的实现。
On some of them it returns std::chrono::time_point<std::chrono::steady_clock>
and in some others it returns std::chrono::time_point<std::chrono::system_clock>
.在其中一些上它返回
std::chrono::time_point<std::chrono::steady_clock>
而在另一些上它返回std::chrono::time_point<std::chrono::system_clock>
。 And that caused problems for me.这给我带来了问题。
A note from cppreference:来自 cppreference 的注释:
Notes
笔记
The high_resolution_clock is not implemented consistently across different standard library implementations, and its use should be avoided.
high_resolution_clock 在不同的标准库实现中的实现不一致,应该避免使用它。 It is often just an alias for std::chrono::steady_clock or std::chrono::system_clock, but which one it is depends on the library or configuration.
它通常只是 std::chrono::steady_clock 或 std::chrono::system_clock 的别名,但它是哪一个取决于库或配置。 When it is a system_clock, it is not monotonic (eg, the time can go backwards).
当它是一个system_clock 时,它不是单调的(例如,时间可以go 向后)。 For example, for gcc's libstdc++ it is system_clock, for MSVC it is steady_clock, and for clang's libc++ it depends on configuration.
例如,对于 gcc 的 libstdc++,它是 system_clock,对于 MSVC,它是 stable_clock,对于 clang 的 libc++,它取决于配置。
Generally one should just use std::chrono::steady_clock or std::chrono::system_clock directly instead of std::chrono::high_resolution_clock: use steady_clock for duration measurements, and system_clock for wall-clock time.
通常,应该直接使用 std::chrono::steady_clock 或 std::chrono::system_clock 而不是 std::chrono::high_resolution_clock:使用 stable_clock 进行持续时间测量,使用 system_clock 进行挂钟时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.