考虑代码:
#include <atomic>
struct stru {
int a{};
int b{};
};
int main() {
// Doesn't work: std::atomic<stru> as({});
std::atomic<stru> as{{}};
}
为直接初始化生成的错误消息如下:
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;
^~~~~~
我认为在这种特殊情况下,这两种形式应该没有区别。为什么会这样?