考虑到这个最小代码示例,我从std::chrono::nanoseconds中构造了一个time_point:
#include <chrono>
int
main()
{
std::chrono::time_point<std::chrono::system_clock> t{std::chrono::nanoseconds{10}};
(void)t;
}
据我所知,按照规范,纳秒是一段时间,time_point支持从时长构造。实际上,在使用x86_64 clang 11.0.0编译时,这段代码可以成功编译:https://godbolt.org/z/jWWzTo。然而,在我的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
有人能帮助我理解,我是在苹果clang编译器中遇到了编译器bug,还是在x86_64 clang中得以不严格支持的东西?
更新
Howard的答案指出了我的问题并提供了解决方案。为了使事情明确,也许避免让其他人感到困惑,以下是我最初的问题:
这个问题的正确答案是“两者都不是”。我最初在Linux上使用libstd c++编写了代码,其中纳秒是time_point的默认持续时间。我没有意识到这一点,但是我的代码隐式地假定了这一点。当我尝试在苹果clang中构建同样的代码,从纳秒持续时间构造time_point时,它无法编译,因为那里的time_point默认为微秒。因此,编译器警告我进行截断,这是有帮助的,肯定不是编译器的bug。一旦我按照Howard的建议明确地使用纳秒实例化所有我的time_points,我在任何系统上都不再有问题。