Topic: [Q] namespaces and one-definition-rule
Author: kuhlins@hawk.wifo.uni-mannheim.de (Stefan Kuhlins)
Date: 11 Nov 1994 08:38:56 GMT Raw View
Please have a look at the following example
and send comments.
Thanks in advance!
- Stefan
// n.h: only declarations!?
namespace A {
class X { /* ... */ };
extern X x; // ok
X y; // error, causes double definition
int i; // ok, but why doesn't it cause an error like X y!?
}
// n.cpp: definitions
#include "n.h"
int A::i = 1;
A::X A::x;
// main.cpp
#include "n.h"
main() {
// use A::i and A::x
}
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 12 Nov 1994 12:14:11 GMT Raw View
>// n.h: only declarations!?
>namespace A {
> class X { /* ... */ };
> extern X x; // ok
> X y; // error, causes double definition
No error yet.
> int i; // ok, but why doesn't it cause an error like X y!?
>}
There's no error yet.
>
>// n.cpp: definitions
>#include "n.h"
>int A::i = 1;
A::i is defined the second time here in the SAME
translation unit. It's an error. The compiler will be
required to issue a diganostic on this one.
>A::X A::x;
>
>// main.cpp
>#include "n.h"
Now y is defined twice in n.cpp and main.cpp. No diagnostic
is required. i is define THREE times: twice in n.cpp and once in main.cpp.
[BTW: my answers are based on my paper on the One Definition Rule.]
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189