A
Arthur J. O'Dwyer
I was paging through Coplien's book "Advanced C++ Programming Styles
and Idioms" this afternoon and found some code that looked something
like
void sort(vector<foo> a)
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a) {
swap(a,a[j]);
++flip;
}
}
} while (flip > 0);
return;
}
Now the question: This code doesn't work on my compiler, because
the 'flip = 0' shadows the declaration of 'flip' outside the 'do'
loop. The 'for' loop increments one 'flip', while the 'do' loop
evaluates another.
Is this how it's supposed to work? (I think yes.)
Did it work a different way when Coplien wrote the book? (I think no.)
And are there online errata for Coplien's book? (I couldn't find any.)
Thanks,
-Arthur
and Idioms" this afternoon and found some code that looked something
like
void sort(vector<foo> a)
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a) {
swap(a,a[j]);
++flip;
}
}
} while (flip > 0);
return;
}
Now the question: This code doesn't work on my compiler, because
the 'flip = 0' shadows the declaration of 'flip' outside the 'do'
loop. The 'for' loop increments one 'flip', while the 'do' loop
evaluates another.
Is this how it's supposed to work? (I think yes.)
Did it work a different way when Coplien wrote the book? (I think no.)
And are there online errata for Coplien's book? (I couldn't find any.)
Thanks,
-Arthur