New syntax for bindings to C++ and namespaces - question

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Nov 3 07:06:41 UTC 2018


On Saturday, November 3, 2018 12:02:19 AM MDT Paul Backus via Digitalmars-d 
wrote:
> On Saturday, 3 November 2018 at 05:49:53 UTC, Cecil Ward wrote:
> > Considering the syntax
> >
> >     extern( C++, "myCppNamespace" ) void cppFunc();
> >
> > If you want to still be able to use the namespace-qualified
> > form myCppNamespace.cppFunc() in your D, even with the string
> > type of reference, what about something like
> >
> >     extern( C++, "myCppNamespace" alias myCppNamespaceAlias )
> >
> > void cppFunc();
>
> Use a separate module:
>
> --- myCppNamespace.d
> module myCppNamespace;
>
> extern(C++, "myCppNamespace") void cppFunc();
>
> --- main.d
> static import myCppNamespace;
>
> void main()
> {
>      myCppNamespace.cppFunc();
> }

Yeah, pretty much the entire point of the new syntax was to make it so that
extern(C++) was _only_ about name mangling, just like extern(C) is only
about name mangling. You can use D's module system to full effect if you
want to organize the symbols rather than stick the entire namespace in the
same file - or you can just use the old extern(C++) syntax. Basically, with
the new extern(C++) syntax, you can organize C++ symbols like you would D
symbols, and that includes using stuff like static imports if that's what
you prefer.

- Jonathan M Davis





More information about the Digitalmars-d mailing list