Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does std::begin deduce a type for braced-init-list?

Tags:

c++

c++11

The following code fails:

template <typename T>
void func(T& t)
{
}

int main()
{
    func({1, 2, 3});
}

But with auto a = {1, 2, 3}; it works because the rules allow auto to deduce an std::initializer_list. How is std::begin written to allow std::begin({1, 2, 3}) to work?

like image 921
user4635155 Avatar asked Mar 17 '23 20:03

user4635155


1 Answers

std::begin({1, 2, 3}) works because std::begin has an overload taking an std::initializer_list.

like image 96
emlai Avatar answered Apr 01 '23 08:04

emlai