November 08, 2015
On Saturday, 7 November 2015 at 21:02:26 UTC, Jeremy DeHaan wrote:
> On Saturday, 7 November 2015 at 14:49:05 UTC, ZombineDev wrote:
>> On Saturday, 7 November 2015 at 14:25:01 UTC, ZombineDev wrote:
>>>
>>> What standard C does not provide and D does: calling C++ free functions nested in namespaces, creating objects of C++ classes (with single inheritance), ...
>> ... calling virtual and non-virtual methods on C++ classes from D
>
> Actually, you can only call virtual methods on classes. Using non-virtual methods isn't supported.

Why do you think so?

See: https://gist.github.com/ZombineDev/19f966273b4a82a5c2f1
Output:

$ g++ --version
g++ (Ubuntu 4.9.2-10ubuntu13) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ dmd --version
DMD64 D Compiler v2.069.0-b2
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
$ ./build.sh
$ ./d_class
cpp.add(40, 2): 42
cpp.mul(1.5, 2f, 'B') 198
$ ./cpp_main
d.add(7, 8): 15
d.mul(3.0, 2.0f, 'A'): 390

November 09, 2015
On Sunday, 8 November 2015 at 23:43:42 UTC, ZombineDev wrote:
> On Saturday, 7 November 2015 at 21:02:26 UTC, Jeremy DeHaan wrote:
>> On Saturday, 7 November 2015 at 14:49:05 UTC, ZombineDev wrote:
>>> On Saturday, 7 November 2015 at 14:25:01 UTC, ZombineDev wrote:
>>>>
>>>> What standard C does not provide and D does: calling C++ free functions nested in namespaces, creating objects of C++ classes (with single inheritance), ...
>>> ... calling virtual and non-virtual methods on C++ classes from D
>>
>> Actually, you can only call virtual methods on classes. Using non-virtual methods isn't supported.
>
> Why do you think so?
>
> See: https://gist.github.com/ZombineDev/19f966273b4a82a5c2f1
> Output:
>
> $ g++ --version
> g++ (Ubuntu 4.9.2-10ubuntu13) 4.9.2
> Copyright (C) 2014 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> $ dmd --version
> DMD64 D Compiler v2.069.0-b2
> Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
> $ ./build.sh
> $ ./d_class
> cpp.add(40, 2): 42
> cpp.mul(1.5, 2f, 'B') 198
> $ ./cpp_main
> d.add(7, 8): 15
> d.mul(3.0, 2.0f, 'A'): 390


Because that's what this page says:
http://dlang.org/cpp_interface.html

It's about halfway down, but here's the section I am referring to says:

Note:

    non-virtual functions, and static member functions, cannot be accessed.

That's awesome if I am wrong, but that is what the C++ interfacing page says.

Declaring it as a struct in D code is freaking genius. I wonder if that works across the board with other compilers and OS's though.


November 09, 2015
On 9/11/2015 4:05 PM, Jeremy DeHaan wrote:
>
>
> Because that's what this page says:
> http://dlang.org/cpp_interface.html
>

That page is out of date.  Virtual and non-virtual member functions, static member functions, and free functions all work since ~2.066.

The biggest missing thing is special member functions, ie ctor/dtor/operators.

> Declaring it as a struct in D code is freaking genius. I wonder if
> that works across the board with other compilers and OS's though.

Mixing struct/class will only work properly with ABIs that mangle them the same way, so it's not portable.
November 09, 2015
On Monday, 9 November 2015 at 05:16:50 UTC, Daniel Murphy wrote:
> On 9/11/2015 4:05 PM, Jeremy DeHaan wrote:
>>
>>
>> Because that's what this page says:
>> http://dlang.org/cpp_interface.html
>>
>
> That page is out of date.  Virtual and non-virtual member functions, static member functions, and free functions all work since ~2.066.
>
> The biggest missing thing is special member functions, ie ctor/dtor/operators.
>
> > Declaring it as a struct in D code is freaking genius. I
> wonder if
> > that works across the board with other compilers and OS's
> though.
>
> Mixing struct/class will only work properly with ABIs that mangle them the same way, so it's not portable.

We should really update that page then.

What is the correct way to use C++ class instances in D? Can you by chance give an example?
November 09, 2015
On 9/11/2015 4:26 PM, Jeremy DeHaan wrote:
>
> What is the correct way to use C++ class instances in D? Can you by
> chance give an example?

extern (C++) class X
{
...
}

extern (C++) void func(X x);

void main(string[] args)
{
    func(new X());
}

etc
November 09, 2015
On Monday, 9 November 2015 at 06:27:22 UTC, Daniel Murphy wrote:
> On 9/11/2015 4:26 PM, Jeremy DeHaan wrote:
>>
>> What is the correct way to use C++ class instances in D? Can you by
>> chance give an example?
>
> extern (C++) class X
> {
> ...
> }
>
> extern (C++) void func(X x);
>
> void main(string[] args)
> {
>     func(new X());
> }
>
> etc

Didn't you say constructors and destructors are missing? What should one do in those cases?
November 09, 2015
On Monday, 9 November 2015 at 06:51:03 UTC, Jeremy DeHaan wrote:
> On Monday, 9 November 2015 at 06:27:22 UTC, Daniel Murphy wrote:
>> On 9/11/2015 4:26 PM, Jeremy DeHaan wrote:
>>>
>>> What is the correct way to use C++ class instances in D? Can you by
>>> chance give an example?
>>
>> extern (C++) class X
>> {
>> ...
>> }
>>
>> extern (C++) void func(X x);
>>
>> void main(string[] args)
>> {
>>     func(new X());
>> }
>>
>> etc
>
> Didn't you say constructors and destructors are missing? What should one do in those cases?

Additionally, should we use new in this case? Wouldn't new create a pointer to a C++ class? Or does it work differently for extern(c++) classes?
November 09, 2015
On 9/11/2015 5:54 PM, Jeremy DeHaan wrote:

>>
>> Didn't you say constructors and destructors are missing? What should
>> one do in those cases?

Constructors and destructors do not match the C++ ABI, but they are still generated, so they can only be called from the language they were written in.  So if you write your classes in D you must new and delete (or GC) them from D.  You can still create them from C++ (or create C++-written classes from D) if you use a forwarding function:

X makeX() { return new X(); }

>
> Additionally, should we use new in this case? Wouldn't new create a
> pointer to a C++ class? Or does it work differently for extern(c++)
> classes?

New in D will allocate a class instance on the GC heap and return a reference to it, just like when it's used with D classes.

These two functions have the same ABI:

// D

extern(C++) class X {}

extern(C++) void func(X x);

// C++

class X {}

void func(X *x);


You can find several examples of C++ interop in dmd's test\runnable\cppa.d
1 2
Next ›   Last »