Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in a brace-enclosed initializer list to a function?

I want to write a function that can be used with an argument that otherwise could directly occur in a range-based loop:

template <typename Iterable>
void sayIt(const Iterable& stuff) {
    for (const auto& e : stuff) {
        cout << e << endl;
    }
}

This works fine with stl containers and other types, but not with a brace-enclosed initializer:

std::vector<std::string> baz(2, "sorry");
sayIt(baz);              // okay
sayIt({"foo", "bar"});   // not okay

Is there a way to make the function work with both?

like image 874
shinjin Avatar asked Mar 03 '23 04:03

shinjin


1 Answers

Braced-init-list has no type and cause template argument deduction failing.

Non-deduced contexts

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

  1. The parameter P, whose A is a braced-init-list, but P is not std::initializer_list, a reference to one (possibly cv-qualified), or a reference to an array:

You can specify the template argument as std::initializer_list explicitly to bypass the deduction,

sayIt<std::initializer_list<std::string>>({"foo", "bar"});

Or add another overload taking std::initializer_list.

template <typename T>
void sayIt(std::initializer_list<T> stuff) {
    sayIt<decltype(stuff)>(stuff);
}
like image 59
songyuanyao Avatar answered May 10 '23 21:05

songyuanyao