Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-4169 std::atomic<T>'s default constructor should be constrained #5128

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions stl/inc/atomic
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,14 @@ template <class _Ty, size_t /* = ... */>
struct _Atomic_storage {
// Provides operations common to all specializations of std::atomic, load, store, exchange, and CAS.
// Locking version used when hardware has no atomic operations for sizeof(_Ty).
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

using _TVal = remove_reference_t<_Ty>;
using _Guard = _Atomic_lock_guard<typename _Atomic_storage_types<_Ty>::_Spinlock>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty>, _Ty, const _TVal> _Value) noexcept
: _Storage(_Value) {
/* implicit */ constexpr _Atomic_storage(const _Ty& _Value) noexcept : _Storage(_Value) {
// non-atomically initialize this atomic
}

Expand Down Expand Up @@ -714,13 +714,13 @@ public:

template <class _Ty>
struct _Atomic_storage<_Ty, 1> { // lock-free using 1-byte intrinsics
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

using _TVal = remove_reference_t<_Ty>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty>, _Ty, const _TVal> _Value) noexcept
: _Storage{_Value} {
/* implicit */ constexpr _Atomic_storage(const _Ty& _Value) noexcept : _Storage{_Value} {
// non-atomically initialize this atomic
}

Expand Down Expand Up @@ -817,13 +817,13 @@ struct _Atomic_storage<_Ty, 1> { // lock-free using 1-byte intrinsics

template <class _Ty>
struct _Atomic_storage<_Ty, 2> { // lock-free using 2-byte intrinsics
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

using _TVal = remove_reference_t<_Ty>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty>, _Ty, const _TVal> _Value) noexcept
: _Storage{_Value} {
/* implicit */ constexpr _Atomic_storage(const _Ty& _Value) noexcept : _Storage{_Value} {
// non-atomically initialize this atomic
}

Expand Down Expand Up @@ -919,13 +919,13 @@ struct _Atomic_storage<_Ty, 2> { // lock-free using 2-byte intrinsics

template <class _Ty>
struct _Atomic_storage<_Ty, 4> { // lock-free using 4-byte intrinsics
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

using _TVal = remove_reference_t<_Ty>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty>, _Ty, const _TVal> _Value) noexcept
: _Storage{_Value} {
/* implicit */ constexpr _Atomic_storage(const _Ty& _Value) noexcept : _Storage{_Value} {
// non-atomically initialize this atomic
}

Expand Down Expand Up @@ -1021,13 +1021,13 @@ struct _Atomic_storage<_Ty, 4> { // lock-free using 4-byte intrinsics

template <class _Ty>
struct _Atomic_storage<_Ty, 8> { // lock-free using 8-byte intrinsics
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

using _TVal = remove_reference_t<_Ty>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty>, _Ty, const _TVal> _Value) noexcept
: _Storage{_Value} {
/* implicit */ constexpr _Atomic_storage(const _Ty& _Value) noexcept : _Storage{_Value} {
// non-atomically initialize this atomic
}

Expand Down Expand Up @@ -1143,12 +1143,15 @@ struct _Atomic_storage<_Ty, 8> { // lock-free using 8-byte intrinsics
#ifdef _WIN64
template <class _Ty>
struct _Atomic_storage<_Ty&, 16> { // lock-free using 16-byte intrinsics
_STL_INTERNAL_STATIC_ASSERT(!is_rvalue_reference_v<_Ty>);

// TRANSITION, ABI: replace '_Ty&' with '_Ty' in this specialization
using _TVal = remove_reference_t<_Ty&>;

_Atomic_storage() = default;

/* implicit */ constexpr _Atomic_storage(conditional_t<is_reference_v<_Ty&>, _Ty&, const _TVal> _Value) noexcept
// TRANSITION, ABI: replace _this_ occurrence of '_Ty&' with 'const _Ty&'
/* implicit */ constexpr _Atomic_storage(_Ty& _Value) noexcept
: _Storage{_Value} {} // non-atomically initialize this atomic

void store(const _TVal _Value) noexcept { // store with sequential consistency
Expand Down Expand Up @@ -2121,10 +2124,11 @@ public:

using value_type = _Ty;

using _Base::_Base;

template <class _Uty = _Ty, enable_if_t<is_default_constructible_v<_Uty>, int> = 0>
constexpr atomic() noexcept(is_nothrow_default_constructible_v<_Ty>) : _Base() {}

/* implicit */ constexpr atomic(const _Ty _Value) noexcept : _Base(_Value) {}

atomic(const atomic&) = delete;
atomic& operator=(const atomic&) = delete;
atomic& operator=(const atomic&) volatile = delete;
Expand Down
13 changes: 12 additions & 1 deletion tests/std/tests/Dev11_0863628_atomic_compare_exchange/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <new>
#include <type_traits>


using namespace std;

#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
Expand Down Expand Up @@ -438,6 +438,17 @@ STATIC_ASSERT(atomic<void*>::is_always_lock_free);
STATIC_ASSERT(atomic<int (*)(int)>::is_always_lock_free);
#endif // _HAS_CXX17

// Also test LWG-4169 std::atomic<T>'s default constructor should be constrained
// (backported to C++14/17 modes as we backported P0883R2)
STATIC_ASSERT(is_default_constructible_v<atomic<int>>);
STATIC_ASSERT(is_default_constructible_v<atomic<bool>>);
STATIC_ASSERT(is_default_constructible_v<atomic<void*>>);
STATIC_ASSERT(is_default_constructible_v<atomic<X>>);
STATIC_ASSERT(is_default_constructible_v<atomic<Y>>);
STATIC_ASSERT(!is_default_constructible_v<atomic<reference_wrapper<int>>>);
STATIC_ASSERT(!is_default_constructible_v<atomic<reference_wrapper<const int>>>);
STATIC_ASSERT(!is_default_constructible_v<atomic<reference_wrapper<int()>>>);


// Also test P0418R2 atomic compare_exchange memory_order Requirements
void test_compare_exchange_relaxed_memory_orders() {
Expand Down
Loading