【问题标题】:Apple clang: Why can I not create a time_point from std::chrono::nanosecondsApple clang:为什么我不能从 std::chrono::nanoseconds 创建 time_point
【发布时间】: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 可以很好地编译:

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

有人可以帮助我了解我是在处理 Apple clang 编译器中的编译器错误,还是解决了 x86_64 clang 中不受严格支持的问题?

更新

霍华德的回答指出了我的问题并提供了解决方案。只是为了让事情明确并且可能避免混淆其他人,这是我最初的问题,从上面引用:

谁能帮助我了解我是否正在处理编译器错误 在 Apple clang 编译器中,或者我是否正在逃避某些事情 在不受严格支持的 x86_64 clang 中?

这个问题的正确答案是“都不是”。我最初在 Linux 上的 libstd c++ 中编写了我的代码,它具有纳秒作为 time_point 的默认持续时间。我没有意识到这一点,但我的代码隐含地假设了这一点。当我尝试在 Apple clang 中构建从纳秒持续时间构建 time_point 的相同代码时,它无法编译,因为 time_point 默认为微秒。因此,编译器警告我截断,这很有帮助,当然不是编译器错误。一旦我按照霍华德的建议做了,并用纳秒显式实例化了我所有的 time_points,我就不再在这两个系统上遇到问题了。

【问题讨论】:

    标签: clang++ chrono


    【解决方案1】:

    在 macOS 上,system_clock::durationmicroseconds。 chrono 库可防止您意外将nanoseconds 截断为microseconds 并丢失信息。您可以手动进行截断(例如使用duration_cast),也可以更改time_point 的类型,以便它可以存储nanoseconds

    std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
        t{std::chrono::nanoseconds{10}};
    

    在 C++20 中,上述行可以简化为:

    std::chrono::sys_time t{std::chrono::nanoseconds{10}};
    

    t 的类型和值将保持不变。 sys_time 只是基于system_clocktime_point 的类型别名。

    【讨论】:

      【解决方案2】:

      time_point 有两个构造函数,它们需要持续时间。

      constexpr explicit time_point(const duration& d);
      template<class Duration2>
        constexpr time_point(const time_point<clock, Duration2>& t);
      

      第一个与时钟提供的持续时间相同。 第二个应该将持续时间转换为时钟提供的持续时间。

      顺便说一句,如果我没看错的话,您是在要求 epoch + 10ns 的开始。 如果 system_clock 的分辨率大于 10ns(确实如此),那么它应该被截断为零,你会得到一个等于 epoch 开始的time_point

      现在,至于为什么这不能编译。 第二个构造函数是基于Duration2 是否可转换为duration(又名system_clock::duration)的SFINAE。

      而且(至少对于 libc++),这似乎是错误的。即,这失败了:

      static_assert (std::is_convertible<
                        std::chrono::nanoseconds,
                        std::chrono::system_clock::duration
                        >
                      ::value, "");
      

      然后问题就变成了 - 应该这是错误的。

      后来:霍华德的回答让我相信这在 Mac OS 上应该是错误的。

      如果您在 Linux 上使用 libstdc++,那么 std::chrono::system_clock::duration 实际上就是 std::chrono::nanoseconds,因此它使用第一个构造函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        • 2017-05-17
        • 1970-01-01
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多