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

4505. The std::make_optional overload taking std::initializer_list should not accept a reference type as its template argument

Section: 22.5.10 [optional.specalg] Status: New Submitter: Jiang An Opened: 2025-12-25 Last modified: 2026-01-17

Priority: Not Prioritized

View all other issues in [optional.specalg].

View all issues with New status.

Discussion:

std::make_optional<std::initializer_list<int>&>({1, 2}) is currently valid, but the returned optional object contains a dangling reference, because the stored reference used to refer to the initializer_list<int> parameter object of that make_optional overload.

Moreover, when there's already an initializer_list<T> variable il, std::make_optional<std::initializer_list<T>&>(il) also returns an optional containing a dangling reference, because the third make_optional overload in 22.5.10 [optional.specalg] is more specialized than the second and thus gets selected.

Given optional<T&> has no specific initializer-list constructor, perhaps we should make the make_optional overload taking initializer_list not accept T& as its first template argument.

Proposed resolution:

This wording is relative to N5032.

  1. Modify 22.5.10 [optional.specalg] as indicated:

    [Drafting note: This also make the implicit SFINAE constraints introduced by 16.3.2.4 [structure.specifications] p4 explicit.]

    template<class T, class U, class... Args>
      constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
    

    -?- Constraints: is_lvalue_reference_v<T> is false and is_constructible_v<T, initializer_list<U>&, Args...> is true.

    -6- Effects: Equivalent to: return optional<T>(in_place, il, std::forward<Args>(args)...);