【发布时间】:2020-12-21 17:08:44
【问题描述】:
鉴于这个最小的代码示例,我从 std::chrono::nanoseconds 构造了一个 time_point:
#include <chrono>
int
main()
{
std::chrono::time_point<std::chrono::system_clock> t{std::chrono::nanoseconds{10}};
(void)t;
}
当我阅读内容时,specification 应该没问题:纳秒是一个持续时间,而 time_point 支持从一个持续时间进行构造。实际上,使用 x86_64 clang 11.0.0 可以很好地编译:
但是,在我的 Mac 上编译时收到以下错误:
clang++ -g -Wall -Werror -std=c++17 test.cc -o test
test.cc:6:54: error: no matching constructor for initialization of 'std::chrono::time_point<std::chrono::system_clock>'
std::chrono::time_point<std::chrono::system_clock> t{std::chrono::nanoseconds{10}};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1355:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::chrono::nanoseconds' (aka 'duration<long long, ratio<1LL, 1000000000LL> >') to
'const std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1, 1000000> > >' for 1st argument
class _LIBCPP_TEMPLATE_VIS time_point
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1355:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::chrono::nanoseconds' (aka 'duration<long long, ratio<1LL, 1000000000LL> >') to
'std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1, 1000000> > >' for 1st argument
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1369:70: note: candidate constructor not viable: no known conversion from 'duration<[...], ratio<[...], 1000000000>>' to 'const duration<[...], ratio<[...], 1000000>>' for 1st argument
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1374:5: note: candidate template ignored: could not match 'time_point' against 'duration'
time_point(const time_point<clock, _Duration2>& t,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1368:61: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
^
1 error generated.
这是我正在使用的clang:
$ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.26.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
有人可以帮助我了解我是在处理 Apple clang 编译器中的编译器错误,还是解决了 x86_64 clang 中不受严格支持的问题?
更新
霍华德的回答指出了我的问题并提供了解决方案。只是为了让事情明确并且可能避免混淆其他人,这是我最初的问题,从上面引用:
谁能帮助我了解我是否正在处理编译器错误 在 Apple clang 编译器中,或者我是否正在逃避某些事情 在不受严格支持的 x86_64 clang 中?
这个问题的正确答案是“都不是”。我最初在 Linux 上的 libstd c++ 中编写了我的代码,它具有纳秒作为 time_point 的默认持续时间。我没有意识到这一点,但我的代码隐含地假设了这一点。当我尝试在 Apple clang 中构建从纳秒持续时间构建 time_point 的相同代码时,它无法编译,因为 time_point 默认为微秒。因此,编译器警告我截断,这很有帮助,当然不是编译器错误。一旦我按照霍华德的建议做了,并用纳秒显式实例化了我所有的 time_points,我就不再在这两个系统上遇到问题了。
【问题讨论】: