Title
std::atomic<X> requires X to be nothrow default constructible
Status
resolved
Section
[atomics.types.generic] [atomics.types.operations]
Submitter
Jonathan Wakely

Created on 2012-07-19.00:00:00 last changed 111 months ago

Messages

Date: 2015-11-04.18:48:42

[ 2015-10, Kona ]

Mark as resolved

Date: 2015-04-09.00:00:00

[ 2015-04-09 Daniel comments ]

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

Date: 2012-11-02.22:48:46

[ 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.

Date: 2012-07-19.00:00:00

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)
History
Date User Action Args
2015-11-04 18:48:42adminsetmessages: + msg7618
2015-11-04 18:48:42adminsetstatus: core -> resolved
2015-04-09 17:13:11adminsetmessages: + msg7341
2012-11-02 22:48:46adminsetmessages: + msg6253
2012-11-02 22:48:46adminsetstatus: new -> core
2012-07-19 00:00:00admincreate