Is there any good reason why C++ namespaces are "closed" in D?
Atila Neves
atila.neves at gmail.com
Mon Jul 30 13:45:33 UTC 2018
On Friday, 27 July 2018 at 22:50:20 UTC, Walter Bright wrote:
> On 7/27/2018 10:28 AM, Atila Neves wrote:
>> But all I'm trying to do here is tell the D compiler how to
>> mangle symbols.
>
> Namespaces have semantic implications, too, such as overload
> resolutions. A namespace introduces a new scope, not just a
> mangling.
Should they, though?
>
> > But why does this not compile?
> > extern(C++, ns) { void foo(); }
> > extern(C++, ns) { void bar(); }
>
> For the same reason that:
>
> struct ns { void foo(); }
> struct ns { void bar(); }
Structs aren't namespaces, I wouldn't expect them to behave the
same.
> doesn't. Being able to crack open a scope and stuff more
> symbols into it at any point in a program is just madness :-)
Perhaps, but that's how C++ works.
>
> However, one can do:
>
> ------ module A ---------
> extern(C++, ns) { void foo(); }
>
> ------ module B ---------
> extern(C++, ns) { void bar(); }
>
> ------ module C ---------
> import A,B;
> ns.foo(); // error, A.ns or B.ns?
> A.ns.foo(); // ok
>
> Because the compiler sees A.ns as utterly distinct from B.ns,
> although the mangling will be the same - any conflicts will be
> the linker's problem.
I didn't know about this and it makes things better, but it's not
a real solution to my problem.
>
> This is how, for example, extern(C) declarations can exist in
> many files.
>
> > Such a program can easily do that to `extern(C)`, but doing
> that to `extern(C++)` is for some reason not allowed.
>
> It is allowed. Just not reopening the same namespace.
I'm arguing that reopening should be allowed.
More information about the Digitalmars-d
mailing list