Why I can not use variable assigned using "initializer_list" as "normal" variable?
code:
void stovr(int a){}
int main() {
auto v {5}; // v is std::initializer_list<int>
stovr(v); // cannot convert 'std::initializer_list<int>' to 'int'
}
v is list )) )v is list and is not int?std::initializer_list<T> cannot convert to T for the same reason that T[] cannot convert to T, and vector<T> cannot convert to T: if you merely know that you have a std::initializer_list<T>, you don't know how many elements it has.
std::initializer_list<int> x = { 1, 2, 3 }; is perfectly valid. If there were an implicit conversion from std::initializer_list<int> to int, which of the values would you expect to see?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With