Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should trivial default constructor respect default member initializer here?

Consider the code:

#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;
}

Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here), while gcc 7.2.0 seems so (see here).

like image 409
Lingxi Avatar asked Mar 20 '18 14:03

Lingxi


1 Answers

Strictly speaking, I think both compilers are right, in that your program exhibits undefined behavior. To quote n4140 (C++14), [atomics.types.operations.req], emphasis mine:

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 is uninitialized before the load. So the usual spiel about undefined behavior must follow.

like image 141
StoryTeller - Unslander Monica Avatar answered Nov 01 '22 23:11

StoryTeller - Unslander Monica