【问题标题】:Why can't a std::atomic of a std::chrono time_point member variable be default constructed?为什么不能默认构造 std::chrono time_point 成员变量的 std::atomic ?
【发布时间】:2020-10-16 16:42:38
【问题描述】:

我有一个原子包装 chrono time_point 值。 time_point 的默认构造对我来说很好,所以我希望不需要显式设置它。但是在 gcc 中,我在没有明确设置默认值的情况下出现编译器错误。这是一个最小的例子:

#include <atomic>
#include <chrono>

struct A
{
    using TimePoint = std::chrono::system_clock::time_point;
    std::atomic<TimePoint> point;
};

int 
main()
{
    A a;
    return 0;
}

这是错误信息:

<source>: In function 'int main()':
<source>:13:7: error: use of deleted function 'A::A()'
     A a;
       ^
<source>:6:5: note: 'A::A()' is implicitly deleted because the default definition would be ill-formed:
     A() = default;
     ^
<source>:6:5: error: use of deleted function 'constexpr std::atomic<_Tp>::atomic() [with _Tp = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >]'
In file included from <source>:1:
/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/atomic:194:7: note: 'constexpr std::atomic<_Tp>::atomic() noexcept [with _Tp = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >]' is implicitly deleted because its exception-specification does not match the implicit exception-specification ''
       atomic() noexcept = default;
       ^~~~~~
Compiler returned: 1

这是一个神螺栓链接: https://godbolt.org/z/YocxGd

我可以通过简单地显式添加默认初始化 (https://godbolt.org/z/8z5WqW) 来解决此问题:

std::atomic<TimePoint> point{TimePoint{}};

但我需要这样做似乎很愚蠢。我不明白出了什么问题。我注意到从 10.x 开始的 clang 和 gcc 不会抱怨隐式默认值。这只是旧版本 gcc 的编译器错误吗?还是有比 time_point 的显式默认初始化更优雅的方式来处理这个问题?


请注意,std::atomic<std::chrono::high_resolution_clock::time_point> can not compile 引用了相同的错误消息,但正在询问(并获得答案)在线程之间共享 time_point 的机制。我对此没有意见。我特别问的是为什么当显式默认构造值起作用时,隐式默认构造值不起作用。

【问题讨论】:

    标签: c++ chrono stdatomic


    【解决方案1】:

    很好的答案@Nicol - 但我将使用 libstdc++ 错误。 以下代码:

    #include <atomic>
    
    struct A {
        A() {}
    };
    
    int main () {
        std::atomic<A> a;
    }
    

    在 gcc 8.3/9.x 上给出了类似的错误,但在 gcc 10.x 上编译没有错误(全部带有 -std=c++17

    clang8 + libstdc++ 8.3 也失败了。

    clang + libc++ 编译没有错误。

    【讨论】:

    • 在我的回答中陈述的条件下,我无法使用我自己的类重现错误。也就是说,包含非noexcept 默认可构造成员的noexcept = default 类型似乎可以默认初始化。所以我想一定是这个问题。
    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 2017-08-12
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多