次のコードが違法なのはなぜですか?
for (int index=0; index<3; index++)
{
cout << {123, 456, 789}[index];
}
これはうまくいきますが:
for (int value : {123, 456, 789})
{
cout << value;
}
IDEOne のコード: http://ideone.com/tElw1w
次のコードが違法なのはなぜですか?
for (int index=0; index<3; index++)
{
cout << {123, 456, 789}[index];
}
これはうまくいきますが:
for (int value : {123, 456, 789})
{
cout << value;
}
IDEOne のコード: http://ideone.com/tElw1w
はをstd::initializer_list提供しませんがoperator[]、 と のオーバーロードがあり、これは forbegin()にend()基づく範囲が使用するものです。initializer_list実際、次のようにインデックスを作成できます。
for (int index=0; index<3; index++)
{
cout << begin({123, 456, 789})[index];
}