Strange warning in C++11 range based for loop

Pavel Dzhioev
Tue Aug 30 13:59:00 GMT 2011

I have some class, that allows to iterate through numeric range.
For example code:

> for (int i: range(N)) {
>    cout << i << endl;
> }

works fine. But if I try repeatedly do some action:

> for (int i: range(N)) {
>    doSomething();
> }

compiler gives me warning:

> warning: unused variable `i' [-Wunused-variable]

But at same time classic for-loop:

> for (int i =3D 0; i < N; ++i) {
>     doSomething();
> }

compiles without warnings.
My compiler's version is "g++-4.6 (GCC) 4.6.1", compiler flags
"--std=c++0x -Wall"
I'm wondering, is it planned behavior or bug?
Thank you for help.
Pavel Sergeev

More information about the Gcc-help mailing list