<div dir="auto"><div><div class="gmail_quote"><div dir="ltr">On Fri., 27 Jul. 2018, 10:30 am Atila Neves via Digitalmars-d, <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I understand that being able to "reopen" namespaces in C++ is <br>
contentious - anybody can add to the `std` namespace in their own <br>
code. D doesn't have anything like it, and instead has packages <br>
and modules. So far, so good.<br>
<br>
But why does this not compile?<br>
<br>
extern(C++, ns) { void foo(); }<br>
extern(C++, ns) { void bar(); }<br>
<br>
I could maybe understand the limitation if those functions had <br>
bodies since we'd be importing the namespace functionality from <br>
C++ in a sense (and even then I'm not sure it's a big enough <br>
deal). But all I'm trying to do here is tell the D compiler how <br>
to mangle symbols.<br>
<br>
Why would this matter? Imagine a project that parses C++ headers <br>
and translates them to D declarations. Imagine that project is <br>
trying to parse `#include <vector>`. There will be many, many <br>
instances of `namespace std` in there, but such a <br>
not-so-hypothetical program can't just go through them and open <br>
and close `extern(C++, std)` as it goes along. Such a program can <br>
easily do that to `extern(C)`, but doing that to `extern(C++)` is <br>
for some reason not allowed.<br>
<br>
(is there even any semantic difference? extern(C) for a 2nd time <br>
is just reopening the global namespace!)<br>
<br>
One could simply manually `pragma(mangle)` everything up the <br>
wazoo, but unfortunately that doesn't work for templates, which, <br>
as it turns out, is pretty much everything inside the `std` <br>
namespace.<br>
<br>
My only solution is to keep track of all namespaces at all times <br>
and then sort the declarations by namespace, which:<br>
<br>
1) Is incredibly tedious<br>
2) Might cause problems with the order of declarations, <br>
especially if macros are involved.<br>
<br>
I can only assume nobody has tried calling a large C++ library <br>
from D (Qt doesn't count, no namespaces). Imagine manually <br>
organising namespaces in one huge bindings file instead of being <br>
able to copy the file layout of the C++ headers!<br>
<br>
Sigh.<br>
<br>
Atila<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">+1</div><div dir="auto"><br></div><div dir="auto">You know how many times I've been upset about this, including the same day that the C++ namespace feature was merged without consulting anyone in the community that intended to use it.</div><div dir="auto"><br></div><div dir="auto">Make a PR that implements namespace as a string... I will use that fork of D forever.</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>