C++ Logo

std-discussion

Advanced search

Re: std::atomic_ref<T> enables mixed size and mixed atomicity accesses which are not defined under C++11 memory model

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Thu, 26 May 2022 13:02:28 +0500
> std::pair<int16_t, int16_t> mixed_size() {
> int32_t x = 0;
>
> {
> auto x_atomic = std::atomic_ref<int32_t>(x);
> x_atomic.store(0xabbafafa, std::memory_order_relaxed);
> // atomic_ref lifetime ends so we can use x again
> }
>
> auto x_parts = reinterpret_cast<int16_t*>(&x);
> int16_t& x_left = x_parts[0];
> int16_t& x_right = x_parts[1];

The last two lines have undefined behavior.

Received on 2022-05-26 08:02:35