Namespaces
Variants
Actions

Talk:cpp/utility/initializer list/initializer list

From cppreference.com

I cannot formulate in words why, but I feel like you shouldn't be able to single-step implicitly create a std::initializer_list<char> from an int without casting.

#include <utility>
 
struct test
{
    test(std::initializer_list<char> a)
    {
        throw a;
    }
};
 
int main()
{
    int a = 9999;
    auto b = test{a};
}

Output:

Program returned: 139
Program stderr
 
terminate called after throwing an instance of 'std::initializer_list<char>'

185.56.101.104 14:18, 11 January 2021 (PST)

Indeed running that code on coliru presents a compiler error --Ybab321 (talk) 08:47, 12 January 2021 (PST)