Title
constinit atomic<shared_ptr<T>> a(nullptr); should work
Status
c++23
Section
[util.smartptr.atomic.shared]
Submitter
Jonathan Wakely

Created on 2022-01-21.00:00:00 last changed 5 months ago

Messages

Date: 2022-02-10.12:58:57

Proposed resolution:

This wording is relative to N4901.

  1. Modify [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;
    […]
    
Date: 2022-02-10.00:00:00

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

Date: 2022-01-15.00:00:00

[ 2022-01-30; Reflector poll ]

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

Date: 2022-01-21.00:00:00

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.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2022-02-10 12:58:57adminsetmessages: + msg12365
2022-02-10 12:58:57adminsetstatus: ready -> wp
2022-01-30 17:01:07adminsetmessages: + msg12310
2022-01-30 17:01:07adminsetstatus: new -> ready
2022-01-22 13:34:40adminsetmessages: + msg12274
2022-01-21 00:00:00admincreate