TITLE: one definition rule diagnosing (Newsgroups: comp.lang.c++.moderated, 10 Nov 97) SCHWINGENSCHLOEGL: Florian Schwingenschloegl >>From the reference manual (Bjarne Stroustrup: The C++ Programming >Language): > >...every use of a particular file scope class name that has been used in >the declaration of an object or function with external linkage or has a >static member or a noninline member function refers to the same class. > >Suppose a program comprises a file containing the declaration > >class X { int i; } x1; > >and another file containing the declaration > >class X { float f; } x2; > > >This ought to be illegal due to the statement above. Nevertheless the >implementation I use accepts it. CLAMAGE: stephen.clamage_nospam@eng.Sun.COM (Steve Clamage) The code violates the One-Definition Rule, and so is invalid. (The ODR says in effect that you can't have different definitions for the same name in the same scope.) Violation of the ODR is an error. Detecting the error means tracking all definitions in all compilation units that wind up being linked into a program -- even in dynamically- linked libraries, details of which are not known until run time. (Suppose dll X contains a global function F, and dll Y contains a global function F with the same signature but different definition. Error? Only if they are both linked into a program at the same time. Maybe that does not happen.) Becasue it is so difficult to catch all such errors, compilers are not required to detect ODR errors across compilation units.