user4635155
user4635155

Reputation: 43

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

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?

Upvotes: 4

Views: 135

Answers (1)

flogram_dev
flogram_dev

Reputation: 42858

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

Upvotes: 7

Related Questions