Search code examples
c++option-typeinitializer-listlibstdc++noexcept

Reference to initializer_list in noexcept specifier of std::optional


I have a question about this code:

    explicit constexpr
    optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
    noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
                        _Args...>)
    : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }

Why is the reference used here? Initializer list is passed to std::optional as value. I guess it might be related with the fact that it is a named argument in this context, but I am not sure.


Solution

  • When you use is_nothrow_constructible and various other type traits, there is a convention that an lvalue reference type T& means "lvalue of type T" whereas a non-reference type T means "rvalue of type T". In this case, a test is being done to see whether _Tp is nothrow constructible given that the first argument will be an lvalue of type initializer_list<_Up>.