C++/CLI for each bug

M

Michel Michaud

Is this a known bug?

int main()
{
System::Collections::ArrayList a;
int i=0;
switch (i)
{
case 0 :
// { /* Would hide the error */
for each (int n in a)
System::Console::WriteLine(n.ToString());
// }
break;
case 1 : // The error is for this line
System::Console::WriteLine("n is not in scope here");
break;
}
}

The error is
error C2360: initialization of '$S1' is skipped by 'case' label

It's as if n would be declared outside of the for each (but it's
scope seems limited to it).

BTW is there an official "known bugs" list somewhere?
 
S

solved by design

Michel Michaud said:
Is this a known bug?

int main()
{
System::Collections::ArrayList a;
int i=0;
switch (i)
{
case 0 : // { /* Would hide the error */
for each (int n in a)
System::Console::WriteLine(n.ToString());
// }
break;
case 1 : // The error is for this line
System::Console::WriteLine("n is not in scope here");
break;
}
}

The error is
error C2360: initialization of '$S1' is skipped by 'case' label

It's as if n would be declared outside of the for each (but it's
scope seems limited to it).

BTW is there an official "known bugs" list somewhere?

You have to put brackets if instantiate vars into a case, at least in C++,
and I suppose in C++/CLI too. I think it is a standard behavior.



--
Visita mi blog: http://rfog.blogsome.com
Libros, ciencia ficción y programación
========================================
La mujer es lo más corruptor y lo más corruptible que hay en el mundo.
-- Confucio. (551-479 a.C.) Filósofo y estadista chino.
 
M

Michel Michaud

In message (e-mail address removed),
solved by design said:
You have to put brackets if instantiate vars into a case, at least
in C++,

No, only if you instantiate them at the case level. Putting brackets
only adds a scope, but a "for" is also a scope, at least in ISO C++
(the rules was not respected in old versions of VC++). For example,
this compiles without problem in ISO C++ and C++/CLI, with VC2005:

int main()
{
int n= 0;

switch (n)
{
case 2 : for (int i=0; i!=n; ++i)
n+= i;
break;
case 0 : ++n;
}
}
and I suppose in C++/CLI too. I think it is a standard behavior.

I don't think so, especially since "for each" is clearly a scope,
like "for". For example try:

int main()
{
System::Collections::ArrayList a;

for each (int n in a)
System::Console::WriteLine(n.ToString());

for each (int n in a)
System::Console::WriteLine(n.ToString());
}

One could also argue that whatever the rules, the error message is
really unclear with its '$S1'...
 
C

Carl Daniel [VC++ MVP]

Michel said:
Is this a known bug?

int main()
{
System::Collections::ArrayList a;
int i=0;
switch (i)
{
case 0 :
// { /* Would hide the error */
for each (int n in a)
System::Console::WriteLine(n.ToString());
// }
break;
case 1 : // The error is for this line
System::Console::WriteLine("n is not in scope here");
break;
}
}

The error is
error C2360: initialization of '$S1' is skipped by 'case' label

It's as if n would be declared outside of the for each (but it's
scope seems limited to it).

BTW is there an official "known bugs" list somewhere?

http://lab.msdn.microsoft.com/productfeedback

This bug is no doubt due to the fact that for-each is implemented in the C++
compiler by token stream re-writing: the front end of the compiler, upon
parsing a for-each construct consumes those token and pushes a translation
of the for-each back into the token stream which is then consumed by the
compiler. That re-writing introduces a local variable without introducing
an implicit scope surrounding the body of the for-each and the definition of
that local variable. Later stages of the compiler are then recognizing the
jump-over-a-declaration based on the re-written token stream, not the
original token stream.

-cd
 
S

solved by design

Michel Michaud said:
In message (e-mail address removed),


No, only if you instantiate them at the case level. Putting brackets
only adds a scope, but a "for" is also a scope, at least in ISO C++
(the rules was not respected in old versions of VC++). For example,
this compiles without problem in ISO C++ and C++/CLI, with VC2005:

int main()
{
int n= 0;

switch (n)
{
case 2 : for (int i=0; i!=n; ++i)
n+= i;
break;
case 0 : ++n;
}
}


I don't think so, especially since "for each" is clearly a scope,
like "for". For example try:

int main()
{
System::Collections::ArrayList a;

for each (int n in a)
System::Console::WriteLine(n.ToString());

for each (int n in a)
System::Console::WriteLine(n.ToString());
}

One could also argue that whatever the rules, the error message is
really unclear with its '$S1'...

Yes, yes, you are right. "for each" is an scope into "case" scope.

--
Visita mi blog: http://rfog.blogsome.com
Libros, ciencia ficción y programación
========================================
Vivir sin filosofar es, propiamente, tener los ojos cerrados, sin tratar de
abrirlos jamás.
-- Descartes. (1596-1650) Filósofo y matemático francés.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top