std::atomic 的标准特化不应该缺少值构造函数吗
Shouldn't standard specializations of std::atomic lack value constructor
std::atomic
模板的通用版本有一个声明为
的值构造函数
constexpr atomic( T desired );
(参见here)
据说bool
,模板的积分和指针特化有(引自cppreference)
standard layout, trivial default constructors, and trivial
destructors. They support aggregate initialization syntax.
这是有道理的,因为 类 只有微不足道的默认 ctor 和 dtor(即没有值 ctor)才有资格作为聚合,因此支持聚合初始化语法。但是,以下代码在 GCC 和 clang 上都可以正常编译:
std::atomic_int i(9);
这意味着应该存在一个值构造函数。这是否违反标准?
引用自 C++11 标准
These specializations shall have standard layout, trivial default
constructors, and trivial destructors. They shall each support
aggregate initialization syntax.
这也没有说明这样的专业化是否应该有一个值 ctor。
标准根据 [atomics.types.generic]:
要求一些类型定义
There shall be named types corresponding to the integral specializations of atomic, as specified in Table 146,
and a named type atomic_bool
corresponding to the specified atomic<bool>
. Each named type is either a
typedef
to the corresponding specialization or a base class of the corresponding specialization. If it is a base
class, it shall support the same member functions as the corresponding specialization.
在 Table 146 中,我们看到 atomic_int
是 atomic<int>
的类型定义。 integral 特化在同一节中定义为:
template <> struct atomic<integral > {
...
constexpr atomic(integral ) noexcept;
...
};
在 int
中用 integral 代替,我们有一个 constexpr atomic_int(int )
构造函数。坦率地说,如果你不能用T
...
初始化atomic<T>
,那会很奇怪
std::atomic
模板的通用版本有一个声明为
constexpr atomic( T desired );
(参见here)
据说bool
,模板的积分和指针特化有(引自cppreference)
standard layout, trivial default constructors, and trivial destructors. They support aggregate initialization syntax.
这是有道理的,因为 类 只有微不足道的默认 ctor 和 dtor(即没有值 ctor)才有资格作为聚合,因此支持聚合初始化语法。但是,以下代码在 GCC 和 clang 上都可以正常编译:
std::atomic_int i(9);
这意味着应该存在一个值构造函数。这是否违反标准?
引用自 C++11 标准
These specializations shall have standard layout, trivial default constructors, and trivial destructors. They shall each support aggregate initialization syntax.
这也没有说明这样的专业化是否应该有一个值 ctor。
标准根据 [atomics.types.generic]:
要求一些类型定义There shall be named types corresponding to the integral specializations of atomic, as specified in Table 146, and a named type
atomic_bool
corresponding to the specifiedatomic<bool>
. Each named type is either atypedef
to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization.
在 Table 146 中,我们看到 atomic_int
是 atomic<int>
的类型定义。 integral 特化在同一节中定义为:
template <> struct atomic<integral > {
...
constexpr atomic(integral ) noexcept;
...
};
在 int
中用 integral 代替,我们有一个 constexpr atomic_int(int )
构造函数。坦率地说,如果你不能用T
...
atomic<T>
,那会很奇怪