2165. std::atomic<X> requires X to be nothrow default constructible

Section: 33.5.8 [atomics.types.generic], [atomics.types.operations] Status: Resolved Submitter: Jonathan Wakely Opened: 2012-07-19 Last modified: 2016-01-28 10:19:27 UTC

Priority: 4

View all other issues in [atomics.types.generic].

View all issues with Resolved status.


As raised in c++std-lib-32781, this fails to compile even though the default constructor is not used:

#include <atomic>

struct X {
  X() noexcept(false) {}
  X(int) { }

std::atomic<X> x(3);

This is because atomic<T>'s default constructor is declared to be non-throwing and is explicitly-defaulted on its first declaration:

atomic() noexcept = default;

This is ill-formed if the implicitly-declared default constructor would not be non-throwing.

Possible solutions:

  1. Add nothrow default constructible to requirements for template argument of the generic atomic<T>
  2. Remove atomic<T>::atomic() from the overload set if T is not nothrow default constructible.
  3. Remove noexcept from atomic<T>::atomic(), allowing it to be deduced (but the default constructor is intended to be always noexcept)
  4. Do not default atomic<T>::atomic() on its first declaration (but makes the default constructor user-provided and so prevents atomic<T> being trivial)
  5. A core change to allow the mismatched exception specification if the default constructor isn't used (see c++std-core-21990)

[2012, Portland: move to Core]

Recommend referring to core to see if the constructor noexcept mismatch can be resolved there. The issue is not specific to concurrency.

[2015-04-09 Daniel comments]

CWG issue 1778, which had been created in behalf of this LWG issue, has been resolved as a defect.

[2015-10, Kona]

Mark as resolved

Proposed resolution: