Title
atomic<T> is unimplementable for non-is_trivially_copy_constructible T
Status
c++20
Section
[atomics.types.generic]
Submitter
Billy O'Neal III

Created on 2017-08-16.00:00:00 last changed 45 months ago

Messages

Date: 2018-11-12.05:21:03

Proposed resolution:

This resolution is relative to N4700.

  1. Edit [atomics.types.generic] as indicated:

    -1- The template argument for T shall meet the CopyConstructible and CopyAssignable requirements. If is_trivially_copyable_v<T> && is_copy_constructible_v<T> && is_move_constructible_v<T> && is_copy_assignable_v<T> && is_move_assignable_v<T> is false, the program is ill-formedbe trivially copyable ([basic.types]). [Note: Type arguments that are not also statically initializable may be difficult to use. — end note]

Date: 2018-11-12.05:21:03

[ 2018-11 San Diego Thursday night issue processing ]

Status to Ready.

Date: 2017-11-15.00:00:00

[ 2017-11-12, Tomasz comments and suggests alternative wording ]

According to my understanding during Albuquerque Saturday issue processing we agreed that we want the type used with the atomics to have non-deleted and trivial copy/move construction and assignment.

Wording note: CopyConstructible and CopyAssignable include semantic requirements that are not checkable at compile time, so these are requirements imposed on the user and cannot be validated by an implementation without heroic efforts.

Date: 2017-11-12.17:44:38

[ 2017-11 Albuquerque Wednesday issue processing ]

Status to Open; Casey and STL to work with Billy for better wording.

Should this include trivially copyable as well as trivially copy assignable?

2017-11-09, Billy O'Neal provides updated wording.

Previous resolution [SUPERSEDED]:

This resolution is relative to N4687.

  1. Edit [atomics.types.generic] as indicated:

    -1- If is_trivially_copy_constructible_v<T> is false, the program is ill-formedThe template argument for T shall be trivially copyable ([basic.types]). [Note: Type arguments that are not also statically initializable may be difficult to use. — end note]

Previous resolution [SUPERSEDED]:

This resolution is relative to N4687.

  1. Edit [atomics.types.generic] as indicated:

    -1- If is_copy_constructible_v<T> is false or if is_trivially_copyable_v<T> is false, the program is ill-formedThe template argument for T shall be trivially copyable ([basic.types]). [Note: Type arguments that are not also statically initializable may be difficult to use. — end note]

Date: 2017-08-16.00:00:00

[atomics.types.generic] requires that T for std::atomic is trivially copyable. Unfortunately, that's not sufficient to implement atomic. Consider atomic<T>::load, which wants to look something like this:

template<class T>
struct atomic {
  __compiler_magic_storage_for_t storage;

  T load(memory_order = memory_order_seq_cst) const {
    return __magic_intrinsic(storage);
  }

};

Forming this return statement, though, requires that T is copy constructible — trivially copyable things aren't necessarily copyable! For example, the following is trivially copyable but breaks libc++, libstdc++, and msvc++:

struct NonAssignable {
  int i;
  NonAssignable() = delete;
  NonAssignable(int) : i(0) {}
  NonAssignable(const NonAssignable&) = delete;
  NonAssignable(NonAssignable&&) = default;
  NonAssignable& operator=(const NonAssignable&) = delete;
  NonAssignable& operator=(NonAssignable&&) = delete;
  ~NonAssignable() = default;
};

All three standard libraries are happy as long as T is trivially copy constructible, assignability is not required. Casey Carter says that we might want to still require trivially copy assignable though, since what happens when you do an atomic<T>::store is morally an "assignment" even if it doesn't use the user's assignment operator.

History
Date User Action Args
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2019-02-26 17:40:23adminsetstatus: voting -> wp
2019-01-21 04:50:04adminsetstatus: ready -> voting
2018-11-12 05:21:03adminsetmessages: + msg10214
2018-11-12 05:21:03adminsetstatus: open -> ready
2017-11-12 17:44:38adminsetmessages: + msg9546
2017-11-10 03:05:33adminsetmessages: + msg9542
2017-11-10 03:05:33adminsetstatus: new -> open
2017-08-19 17:42:54adminsetmessages: + msg9449
2017-08-16 00:00:00admincreate