Yanko
2008-Oct-02 15:12 UTC
[LLVMdev] MS C++ gives error C2371 on this code while (obviously) gcc compiles it fine
Taken from tools/llvmc2/CompilationGraph.cpp: ... for (typename C::const_iterator B = EdgesContainer.begin(), E = EdgesContainer.end(); B != E; ++B) { const Edge* E = B->getPtr(); ... MS C++ compiler (VS 2008) gives: ... CompilationGraph.cpp ..\..\..\llvm\tools\llvmc2\CompilationGraph.cpp(58) : error C2371: 'E' : redefinition; different basic types ..\..\..\llvm\tools\llvmc2\CompilationGraph.cpp(57) : see declaration of 'E' ... While GCC compiles it fine. (I'm assuming it does, 'cause almost everyone here uses gcc) Changing the code to ... for (typename C::const_iterator B = EdgesContainer.begin(), End = EdgesContainer.end(); B != End; ++B) { const Edge* E = B->getPtr(); ... makes the code compilable by MS C++. But as a curiosity (and I really don't know the answer because I can barely read C++): Which compiler got it right?
Jay Freeman (saurik)
2008-Oct-02 17:34 UTC
[LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
gcc is correct. According to the ISO specification, the for-init-statement is supposed to inject any variable names into the same declarative scope as the condition of an equivalent restructuring of the loop in the form of a while statement, which in turn fronts the declaration to an extra scope that surrounds the /entire/ loop construct. VC++ seems to be scoping the variables as if they were /inside/ of the loop and not creating this extra scope. Frowny. -J -------------------------------------------------- From: "Yanko" <yhdezalvarez at gmail.com> Sent: Thursday, October 02, 2008 8:12 AM To: <llvmdev at cs.uiuc.edu> Subject: [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine ...> makes the code compilable by MS C++. But as a curiosity (and I really > don't know the answer because I can barely read C++): Which compiler > got it right?
OvermindDL1
2008-Oct-02 18:16 UTC
[LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
On Thu, Oct 2, 2008 at 11:34 AM, Jay Freeman (saurik) <saurik at saurik.com>wrote:> gcc is correct. According to the ISO specification, the for-init-statement > is supposed to inject any variable names into the same declarative scope as > the condition of an equivalent restructuring of the loop in the form of a > while statement, which in turn fronts the declaration to an extra scope > that > surrounds the /entire/ loop construct. VC++ seems to be scoping the > variables as if they were /inside/ of the loop and not creating this extra > scope. Frowny. -JThis can be changed in the project settings for a VC++ project. I think it was called something like "Enforce for loop conformance" or so... This is something I always change though. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20081002/1f86c6e8/attachment.html>
Argiris Kirtzidis
2008-Oct-02 18:33 UTC
[LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
Jay Freeman (saurik) wrote:> gcc is correct. According to the ISO specification, the for-init-statement > is supposed to inject any variable names into the same declarative scope as > the condition of an equivalent restructuring of the loop in the form of a > while statement, which in turn fronts the declaration to an extra scope that > surrounds the /entire/ loop construct. VC++ seems to be scoping the > variables as if they were /inside/ of the loop and not creating this extra > scope. Frowny. -J >Actually, gcc is wrong and VC++ got it right. From the C++ standard, 6.4p3:> A name introduced by a declaration in a condition (either introduced > by the type-specifier-seq or the declarator of the > condition) is in scope from its point of declaration until the end of > the substatements controlled by the condition. If the > name is re-declared in the outermost block of a substatement > controlled by the condition, the declaration that re-declares > the name is ill-formed.Which gives us: while (int x=0) { int x=0; // error: redeclaration, clashes with condition } Both gcc and VC++ emit a compilation error for the above. Then the standard says this, 6.5.3p1:> names declared in the for-init-statement are in the same > declarative-region as those declared in the conditionSo names inside the 'for' loop clash with both the condition and the for-init-statement: for (int x=0;;) { int x=0; // error: redeclaration, clashes with for-init-statement } but gcc, incorrectly, does not emit a compilation error. And while we are on the subject, gcc is also wrong on this one: if (int x=0) { int x=0; // error: redeclaration, but gcc does not emit any errors. } -Argiris> -------------------------------------------------- > From: "Yanko" <yhdezalvarez at gmail.com> > Sent: Thursday, October 02, 2008 8:12 AM > To: <llvmdev at cs.uiuc.edu> > Subject: [LLVMdev] MS C++ gives error C2371 on this code while > (obviously)gcc compiles it fine > > ... > >> makes the code compilable by MS C++. But as a curiosity (and I really >> don't know the answer because I can barely read C++): Which compiler >> got it right? >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >
Possibly Parallel Threads
- [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
- [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
- [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
- [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine
- [LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine