array<int> ^ints = gcnew array<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for each(int i in ints)
if(i % 2 == 0)
Debug::WriteLine("Even\n");
else
Debug::WriteLine("Odd\n");
Why does the above fail to compile? It works fine if I use a for(int i; ...)
or if I enclose the if-else
within braces. I know that the manual clearly indicates that braces are required, but I want to know why this departure from expected behaviour?
为什么以上都无法编译?如果我使用for(int i; ...)或者将if-else括在大括号中,它可以正常工作。我知道手册清楚地表明需要括号,但我想知道为什么这会偏离预期的行为?
2 个解决方案
#1
I can't find any official documentation on it, but I can confirm the behavior you're seeing. It simply appears that the compiler is overly aggressive about finding the single statement following the for each
, to the point of splitting the else
from the if
. It compiles it as if you had written the following, which is clearly incorrect.
我找不到任何关于它的官方文档,但我可以确认你看到的行为。只是看起来编译器过于积极地找到跟随for的每个单个语句,以及将if与if分开。它编写它就好像你写了以下内容,这显然是不正确的。
array<int> ^ints = gcnew array<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for each(int i in ints)
{
if(i % 2 == 0)
Debug::WriteLine("Even\n");
}
else
Debug::WriteLine("Odd\n");
This is obviously different to how a regular C++ for
loop behaves, so you may wish to file a bug at http://connect.microsoft.com.
这显然不同于常规C ++ for循环的行为,因此您可能希望在http://connect.microsoft.com上提交错误。
#2
I'm not a CLI export but I believe that
for each (i in ints)
is what you are after.
我不是CLI导出,但我相信每个(我在ints)就是你所追求的。
Edit
I checked the C++ source that I have available and we are using:
我检查了我可用的C ++源代码,我们正在使用:
for each (int i in ints) {
// body
}
Is it possible that the braces are required? I don't have an MSFT compiler handy or I would try the following:
是否可能需要牙箍?我没有方便的MSFT编译器,或者我会尝试以下方法:
for each (int i in ints) {
if(i % 2 == 0) {
Debug::WriteLine("Even\n");
} else {
Debug::WriteLine("Odd\n");
}
}
#1
I can't find any official documentation on it, but I can confirm the behavior you're seeing. It simply appears that the compiler is overly aggressive about finding the single statement following the for each
, to the point of splitting the else
from the if
. It compiles it as if you had written the following, which is clearly incorrect.
我找不到任何关于它的官方文档,但我可以确认你看到的行为。只是看起来编译器过于积极地找到跟随for的每个单个语句,以及将if与if分开。它编写它就好像你写了以下内容,这显然是不正确的。
array<int> ^ints = gcnew array<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for each(int i in ints)
{
if(i % 2 == 0)
Debug::WriteLine("Even\n");
}
else
Debug::WriteLine("Odd\n");
This is obviously different to how a regular C++ for
loop behaves, so you may wish to file a bug at http://connect.microsoft.com.
这显然不同于常规C ++ for循环的行为,因此您可能希望在http://connect.microsoft.com上提交错误。
#2
I'm not a CLI export but I believe that
for each (i in ints)
is what you are after.
我不是CLI导出,但我相信每个(我在ints)就是你所追求的。
Edit
I checked the C++ source that I have available and we are using:
我检查了我可用的C ++源代码,我们正在使用:
for each (int i in ints) {
// body
}
Is it possible that the braces are required? I don't have an MSFT compiler handy or I would try the following:
是否可能需要牙箍?我没有方便的MSFT编译器,或者我会尝试以下方法:
for each (int i in ints) {
if(i % 2 == 0) {
Debug::WriteLine("Even\n");
} else {
Debug::WriteLine("Odd\n");
}
}