Resend for easy reading. Thanks! On 2018-01-16 14:45, Andrew Pinski wrote: > On Mon, Jan 15, 2018 at 10:35 PM, chenzhelu <chenz...@netease.com> wrote: >> Hello all, >> I encountered a problem on "local class name conflict", >> I searched on net and found that years ago, some people also encoutered this >> kind of problem. > > The correct name for this is One Definition Rule (or ODR for short). > Basically a violation of this rule causes the code to be invalid (with > no diagnostic required; therefor undefined code). If you use LTO, GCC > sometimes will warn about this violation.
Thanks for suggestion! Yes, this test code is intent to create local classes with same name, for a small project, it's easy to find out this error, consider a big project... The root cause is, it's possible that two local classes with same name in a namespace, (or the default anonymous namespace), I know that it violate with ODR, however, the point is, it seems that compiler is somehow not consistent in the Compile and Link phase, that's what I confused of. > > If you want a truly local class, then use anonymous namespaces which > is designed to fix this issue. This will rely on human to keep local class name unique within a namspace, or the default anonymous namespace. consider a project with hundreds of files. As a precise software, I think it'd better not allow this undefined behavior open, whether the compile better give some warning/error on this name conflicts, or link with the "nearest" local class ? Thanks! > > Thanks, > Andrew > >> >> https://stackoverflow.com/questions/10671956/same-class-name-in-different-c-files >> http://www.cplusplus.com/forum/general/32010/ >> >> >> Please see the following code, (BTW, it seems that the maillist can not >> attach files) >> >> When I ran the code(Ubuntu 64bis, gcc vesion:5.4.0, the amazing result is : >> localClassXInData.cpp in Data.cpp >> localClassXInData.cpp in Data.cpp >> >> >> I think there is an error in Link phrase: >> 1. if comment out the LocalClassX definition in main.cpp, the compile will >> not compile the code --- this is as expected. >> 2. however, if run the code(not comment out any code), the code actually ran >> is in the Data.cpp, that is not consistent with compile phase. >> >> >> Thanks!