琐碎的默认构造函数应该在这里尊重默认成员初始值设定项吗?

Should trivial default constructor respect default member initializer here?

考虑代码:

#include <atomic>
#include <iostream>

struct stru {
  int a{};
  int b{};
};

int main() {
  std::atomic<stru> as;
  auto s = as.load();
  std::cout << s.a << ' ' << s.b << std::endl;
}

请注意,尽管 stru 具有默认成员初始值设定项,但自 C++14 起它仍然符合聚合类型的条件。 std::atomic 有一个简单的默认构造函数。按照标准,as的成员应该初始化为零吗? clang 6.0.0 不这样做(参见 here), while gcc 7.2.0 seems so (see here)。

严格来说,我认为这两个编译器都是正确的,因为您的程序表现出未定义的行为。引用 n4140 (C++14),[atomics.types.operations.req],强调我的:

In the following operation definitions:

  • an A refers to one of the atomic types.

[...]

A::A() noexcept = default;

Effects: leaves the atomic object in an uninitialized state. [ Note: These semantics ensure compatibility with C. — end note ]

as 在加载之前未初始化。所以关于未定义行为的通常的高谈阔论必须遵循。

根据 cppreferencestd::atomic::atomic() 构造函数没有初始化对象:

atomic() noexcept = default;

1) The default constructor is trivial: no initialization takes place other than zero initialization of static and thread-local objects. std::atomic_init may be used to complete initialization.