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

Steven Schveighoffer schveiguy at gmail.com
Thu Aug 2 12:24:35 UTC 2018


On 8/2/18 5:26 AM, Daniel N wrote:
> On Thursday, 2 August 2018 at 08:31:28 UTC, Walter Bright wrote:
>> On 8/1/2018 10:06 PM, Johannes Pfau wrote:
>>> I guess that would be acceptable if there is a real benefit, but I have
>>> not seen a single argument for the current behavior in this thread.
>>
>> Please read my postings in this thread, then.
> 
> I know you have been very patient with explaining this, but I've been 
> trying to read all threads on this topic and I'm still not sure, can you 
> please acknowledge if I understood you correctly or not?
> 
> Let's use a simple pseudo example? Let's assume we have two different 
> 'any' implementations...
> 
> The current extern(c++) design allows us to bind to both of them in a 
> single file.
> 
> cpp_bindings.d
> extern(c++, std)   ...any-definition...
> extern(c++, boost) ...any-definition...
> 
> Personally I would never *bind* to two different namespaces in a single 
> file, are there any other additional benefits to the current design 
> which I'm overlooking?
> 
> With a non-scoped extern(c++) we could simply use two files.
> 
> cppstd/any.d
> extern(c++, std)   ...any-definition...
> 
> boost/any.d
> extern(c++, boost) ...any-definition...
> 
> My conclusion is that _if_ I understood you correctly, it must mean that 
> in the examples which you have in mind, it is common to *bind* to two 
> namespaces in the same file? Could you give a real-world examples of two 
> namespaces which you would like to mix like that in the same *bindings* 
> file? Is it for instance 'std' and 'std::experimental'?
> 

The example that Walter keeps bringing up is one where the C++ code has 
2 namespaces in the same header file. Not only that, but then 
identically named functions in those namespaces. In that case, a direct 
translation would cause problems with hijacking in D, where it doesn't 
in C++ (as long as you don't ever use `using` declarations, or only use 
one or the other).

So the difference from your example is that you're not trying to bind 2 
different files with 2 different namespaces into one D module, but you 
are translating a single C++ header file that's written with the 2 
bindings as described. Not std and boost, but ns1 and ns2 inside the 
same header file, which each have identical symbols.

I've never seen it, but it's certainly valid C++ and in the realm of 
possibility.

-Steve


More information about the Digitalmars-d mailing list