Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert 'std::initializer_list<int>' to 'int'

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'

}
  • Why there is not implicit conversation? (edit: becausev is list )) )
  • Why v is list and is not int?
like image 610
Person.Junkie Avatar asked Dec 14 '25 07:12

Person.Junkie


1 Answers

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?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!