Is there any good reason why C++ namespaces are "closed" in D?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Aug 4 17:54:11 UTC 2018


On Saturday, August 04, 2018 12:18:21 tide via Digitalmars-d wrote:
> The only person I've seen that wants this is Walter. I haven't
> seen anyone else show interest in wanting a 1:1 correlation. It's
> unreasonable, D isn't C++ nor should it be trying to strive to be
> C++.

In general, it's easier for maintenance if there's a 1:1 correlation between
C/C++ headers and D headers. It makes it easier to update the D files, and
it makes it easier to find where something is in the D translation if you're
already familiar with the C/C++ version - or if you're reading documentation
for the C/C++ version and trying to translate that into what you need to
know to use the D version. For the most part, the C bindings in druntime
follow the layout of the C headers that they correspond to precisely because
doing so is more maintainable and easier to navigate than if the D modules
were laid in a hierarchy that followed its own logic instead of trying to
emulate the layout of the C headers being translated. In fact, some of the
more annoying issues with the C header translations in druntime come from
places where the layout of the C headers was not quite followed for one
reason or another.

So, I'd fully expect most C/C++ binding projects to have a 1:1 correlation
between C/C++ headers and D headers, and I don't think that Walter is at all
misguided in that respect.

That being said, in most cases, such a 1:1 translation would work just fine
if we had extern(C++, "NS"), and it just affected name mangling. There would
be corner cases where it did not, but they could be worked around even if it
meant that in that particular case, you lost the 1:1 translation - but as
Steven pointed out, it actually would be pretty straightforward to manually
add the kind of namespace wrappers that the language currently forces on you
with extern(C++, NS) if we had extern(C++, "NS"). And of course, if we
actually ended up with both extern(C++, "NS"), and extern(C++, NS), then you
could just use the extern(C++, NS) construct in those corner cases, though
ultimately, I think that we'd be better off with just having
extern(C++, "NS").

So, I don't think that Walter is being misguided here in terms of how he
expects folks to be translating C++ headers. I just think that his concern
over some corner cases you get with bad C++ code has caused him to make the
current solution for binding to C++ namespaces more complicated than it
needs to be. And I don't think that his concerns are misguided either. I
just think that he's just blowing them out of proportion given how easy it
would be to work around those issues with the solution that Manu wants,
whereas the common case suffers with the current solution.

- Jonathan M Davis



More information about the Digitalmars-d mailing list