Skip to content

<atomic>: atomic_ref<const T> fails to compile #1497

Description

@mocabe

Description
n4868/[atomic.ref.generic] says template parameter T of atomic_ref<T> should satisfy is_trivially_copyable_v.
Current atomic_ref<T> has static_assert for is_trivially_copyable_v<_Ty> && is_copy_constructible_v<_Ty> && is_move_constructible_v<_Ty> && is_copy_assignable_v<_Ty> && is_move_assignable_v<_Ty>, which is probably copy-pasted from atomic<T>.

This makes atomic_ref<const T> fail to compile since const T is not assignable, but I believe this is valid use case.

Test code

#include <atomic>

auto load_as_atomic(const std::atomic<int>& i) 
{
    return i.load();
}

auto load_as_atomic(const int& i) 
{
    // should compile
    return std::atomic_ref(i).load();
}

int main() {}

This compiles with libstdc++ (Godbolt), but not with STL.

Possible Solution

Change static_assert to match standard wording.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions