https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115402
Bug ID: 115402
Summary: std::atomic_ref<volatile T> compile-error in
compare_exchange_[weak/strong]() and wait()
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: lewissbaker.opensource at gmail dot com
Target Milestone: ---
See https://godbolt.org/z/q4jYvPaah
The following code-snippet fails to compile:
volatile int vi = 0;
std::atomic_ref<volatile int> vref(vi);
int val = vref.load(); // ok
vref.exchange(val); // ok
vref.compare_exchange_weak(val, 0); // error
vref.compare_exchange_strong(val, 0); // error
vref.wait(0); // error
Fails with error messages like:
.../include/c++/15.0.0/bits/atomic_base.h: In instantiation of 'bool
std::__atomic_ref<_Tp, true, false>::compare_exchange_weak(_Tp&, _Tp,
std::memory_order, std::memory_order) const [with _Tp = volatile int]':
/opt/compiler-explorer/gcc-trunk-20240609/include/c++/15.0.0/bits/atomic_base.h:1674:30:
required from 'bool std::__atomic_ref<_Tp, true,
false>::compare_exchange_weak(_Tp&, _Tp, std::memory_order) const [with _Tp =
volatile int]'
1674 | return compare_exchange_weak(__expected, __desired, __order,
| ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1675 | __cmpexch_failure_order(__order));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:8:31: required from here
8 | vref.compare_exchange_weak(val, 0); // error
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
.../include/c++/15.0.0/bits/atomic_base.h:1656:58: error: binding reference of
type 'std::__atomic_impl::_Val<volatile int>&' {aka 'int&'} to 'volatile int'
discards qualifiers
1656 | return __atomic_impl::compare_exchange_weak<true>(
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
1657 | _M_ptr, __expected, __desired, __success, __failure);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.../include/c++/15.0.0/bits/atomic_base.h:1119:52: note: initializing
argument 2 of 'bool std::__atomic_impl::compare_exchange_weak(_Tp*, _Val<_Tp>&,
_Val<_Tp>, std::memory_order, std::memory_order, bool) [with bool _AtomicRef =
true; _Tp = volatile int; _Val<_Tp> = int]'
1119 | compare_exchange_weak(_Tp* __ptr, _Val<_Tp>& __expected,
| ~~~~~~~~~~~^~~~~~~~~~