13

Consider the code:

#include <atomic>

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

int main() {
  // Doesn't work: std::atomic<stru> as({});
  std::atomic<stru> as{{}};
}

The error message produced for direct initialization is like:

prog.cc: In function 'int main()':
prog.cc:9:26: error: call of overloaded 'atomic(<brace-enclosed initializer list>)' is ambiguous
   std::atomic<stru> as({});
                          ^
In file included from prog.cc:1:0:
/opt/wandbox/gcc-7.2.0/include/c++/7.2.0/atomic:200:17: note: candidate: constexpr std::atomic<_Tp>::atomic(_Tp) [with _Tp = stru]
       constexpr atomic(_Tp __i) noexcept : _M_i(__i) { }
                 ^~~~~~
/opt/wandbox/gcc-7.2.0/include/c++/7.2.0/atomic:196:7: note: candidate: std::atomic<_Tp>::atomic(const std::atomic<_Tp>&) [with _Tp = stru] <deleted>
       atomic(const atomic&) = delete;
   ^~~~~~

I think the two forms should be no different in this particular case. Why is this happening?

Boann
  • 48,794
  • 16
  • 117
  • 146
Lingxi
  • 14,579
  • 2
  • 37
  • 93

0 Answers0