extern(C++, ns)

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 20 12:56:49 PST 2016


On 1/20/2016 4:17 AM, Manu via Digitalmars-d wrote:
> -- name.x.d--------------------
> module name.x;
> -- name.y.d--------------------
> module name.y;
> import name.x;
> extern(C++, name) int x;
> ------------------------------------

This won't work any more than:

   import name.y;
   struct name { }

would. You can't have two different definitions for the same identifier in the 
same scope, and that's true for any programming language I've heard of.


> ------------------------------------
> extern(C++, delegate) int x;
> ------------------------------------

Yes, we've talked about that. I'm thinking of a solution where it could be 
written like:

     extern(C++, delegate_) int x;

and the C++ name mangler strips off any trailing _.

Note that this problem is NOT resolved by your proposed solution, as this 
wouldn't work, either:

    extern(C++) int* delegate();

or any other use of D keywords as C/C++ identifiers.

     https://issues.dlang.org/show_bug.cgi?id=15587


> You claim this is a problem that must desperately be solved:
> ------------------------------------
> int x;
> extern(C++, ns) int x;
> ------------------------------------
>
> The solution arrives at basically the same problem:
> ------------------------------------
> int ns;
> extern(C++, ns) int x;
> ------------------------------------
>
> We've just robbed peter to pay paul?

C++ doesn't allow:

     int ns;
     namespace ns { ... }

either, so there can never be a need to solve that problem.


> scanning reflection
> needs to be retrofit with new tricks to recurse into namespaces:
> ------------------------------------
> extern(C++, ns) void f();
> pragma(msg, __traits(allMembers, mixin(__MODULE__)));
> ------------------------------------
>> tuple("object", "ns")
>
> ... f() is not present.
>
> By extension of this, extern(C++, ns) declarations are incompatible
> with every single implementation of scanning reflection I've written
> in the past 6 years, and everyone else's too.
> All such code needs to be retrofit with new tricks to recurse into ns.

Seems to me that all that is necessary is to replace:

   __traits(allMembers, mixin(__MODULE__))

with:

    getAllMembersIncludingNs(mixin(__MODULE__))

and then write getAllMembersIncludingNS() as a function that recursively adds 
members of namespaces. After all, the ugly __traits() was always meant to be a 
building block to be encapsulated by a function that does what is really wanted. 
I wouldn't call it tricky.


More information about the Digitalmars-d mailing list