This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++23 status.

3661. constinit atomic<shared_ptr<T>> a(nullptr); should work

Section: 33.5.8.7.2 [util.smartptr.atomic.shared] Status: C++23 Submitter: Jonathan Wakely Opened: 2022-01-21 Last modified: 2023-11-22

Priority: Not Prioritized

View all other issues in [util.smartptr.atomic.shared].

View all issues with C++23 status.

Discussion:

All the following are valid except for the last line:

constinit int i1{};
constinit std::atomic<int> a1{};
constinit int i2{0};
constinit std::atomic<int> a2{0};
constinit std::shared_ptr<int> i3{};
constinit std::atomic<std::shared_ptr<int>> a3{};
constinit std::shared_ptr<int> i4{nullptr};
constinit std::atomic<std::shared_ptr<int>> a4{nullptr}; // error

The initializer for a4 will create a shared_ptr<int> temporary (using the same constructor as i4) but then try to use atomic(shared_ptr<int>) which is not constexpr.

This is an unnecessary inconsistency in the API for atomic<shared_ptr<T>> that can easily be fixed. The proposed resolution has been implemented in libstdc++.

There is no need to also change atomic<weak_ptr<T>> because weak_ptr doesn't have a constructor taking nullptr.

[2022-01-30; Reflector poll]

Set status to Tentatively Ready after seven votes in favour during reflector poll.

[2022-02-10 Approved at February 2022 virtual plenary. Status changed: Tentatively Ready → WP.]

Proposed resolution:

This wording is relative to N4901.

  1. Modify 33.5.8.7.2 [util.smartptr.atomic.shared], class template partial specialization atomic<shared_ptr<T>> synopsis, as indicated:

    […]
    constexpr atomic() noexcept;
    constexpr atomic(nullptr_t) noexcept : atomic() { }
    atomic(shared_ptr<T> desired) noexcept;
    atomic(const atomic&) = delete;
    void operator=(const atomic&) = delete;
    […]