How to pass a class by (const) reference to C++

2021-12-12 Thread Jan via Digitalmars-d-learn
In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that takes A by const reference: ```cpp void CppFunc(const A& arg); ``` But how do I bind this in D ? ```cpp extern(C++) void CppFunc(A arg); // tries to pass as 'A*' extern(C++

Re: How to pass a class by (const) reference to C++

2021-12-12 Thread evilrat via Digitalmars-d-learn
On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that takes A by const reference: ```cpp void CppFunc(const A& arg); ``` But how do I bind this in D ? ```cpp extern(C++)

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that takes A by const reference: ```cpp void CppFunc(const A&

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Monday, 13 December 2021 at 11:13:12 UTC, Tejas wrote: On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A {

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: class _A {} struct A {} With ```extern(C++)``` on these…

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics this shim logic is a better solution in such cases. Literally anything

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:51:17 UTC, evilrat wrote: That example is still looks very conspicuous because it is very likely does nothing on the caller side in C++ as it passes a copy. Yes, I wouldn't want to use it, maybe manual mangling is better, but still painful. ```const A&``` is

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 13:02:50 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:51:17 UTC, evilrat wrote: That example is still looks very conspicuous because it is very likely does nothing on the caller side in C++ as it passes a copy. Yes, I wouldn't want to use it

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tim via Digitalmars-d-learn
On Monday, 13 December 2021 at 15:21:19 UTC, Jan wrote: On Monday, 13 December 2021 at 13:02:50 UTC, Ola Fosheim Grøstad wrote: Yes, I wouldn't want to use it, maybe manual mangling is better, but still painful. ```const A&``` is so common in C++ API's that it really should be supported out-of-

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 16:29:12 UTC, Tim wrote: I made a pull request, which changes the mangling to tail const for classes passed directly as parameter or return type, but now think this would break too much code: https://github.com/dlang/dmd/pull/13369 The proposal to add a deref-type

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tim via Digitalmars-d-learn
On Monday, 13 December 2021 at 21:17:49 UTC, Jan wrote: Unfortunately no. Maybe the cast would even make it work, but I can't have "A" and "_A" in D as separate types, because the class is supposed to link to C++ functions and thus renaming it from A to _A breaks that. On the other hand I can't

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: On Monday, 13 December 2021 at 11:13:12 UTC, Tejas wrote: On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: [...] You'll have to use something called a [shim](https://en.wikipedia.org/wiki/Shim_(computing)), it seems. For e

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 06:21:39 UTC, Tejas wrote: Hey, evilrat, I've seen people make claims that our C++ interop has reached phenomenal levels and that going any further would basically require a C++ compiler ala ImportC++, the issue is just that the docs haven't been updated yet to

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 07:50:48 UTC, evilrat wrote: There is some missing features like above tail ref and const, there is minor mangling issues that requires pragma mangle sometimes, and other annoying little details. As far as I can tell from my limited experience, the way D approa

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 09:36:54 UTC, Jan wrote: Unfortunately it's the "annoying little details" that I immediately bumped into. Just another example: I just learned that linking against C++ DLLs is quite limited. I ran into the issue that linking in an external variable doesn't wo

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread rikki cattermole via Digitalmars-d-learn
On 15/12/2021 11:54 PM, Jan wrote: On Wednesday, 15 December 2021 at 09:36:54 UTC, Jan wrote: Unfortunately it's the "annoying little details" that I immediately bumped into. Just another example: I just learned that linking against C++ DLLs is quite limited. I ran into the issue that linki

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 11:03:27 UTC, rikki cattermole wrote: On 15/12/2021 11:54 PM, Jan wrote: On Wednesday, 15 December 2021 at 09:36:54 UTC, Jan wrote: Unfortunately it's the "annoying little details" that I immediately bumped into. Just another example: I just learned that lin

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread evilrat via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 12:02:08 UTC, Jan wrote: On Wednesday, 15 December 2021 at 11:03:27 UTC, rikki cattermole wrote: On 15/12/2021 11:54 PM, Jan wrote: On Wednesday, 15 December 2021 at 09:36:54 UTC, Jan wrote: Unfortunately it's the "annoying little details" that I immediately

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 10:54:45 UTC, Jan wrote: Someone with more in-depth knowledge told me, that Windows support in D and specifically DLL support is lacking quite a bit. gdc and ldc have the same full support you'd expect coming from microsoft c++. dmd doesn't though. You can

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 12:36:49 UTC, evilrat wrote: You probably know this but just in case - unlike C++ in D variables by default have thread local storage, to link with C++ global variable you need to use __gshared storage modifier in D, it is similar to 'shared' variable that unli

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Tim via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 15:20:18 UTC, Jan wrote: As I was told linking against functions in DLLs works perfectly, because it is identical to static linking. Linking against global/static variables supposedly differs and that's not handled correctly for DLLs. As I said, I talked to som

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 17:10:51 UTC, Tim wrote: Do you have a test case for your problem? C++ ```cpp class Test { public: __declspec(dllexport) static int var; }; int Test::var = 42; ``` D ```cpp extern(C++, class) struct Test { extern (C++) export extern __gshared int var; } `

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Tim via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 19:28:44 UTC, Jan wrote: C++ ```cpp class Test { public: __declspec(dllexport) static int var; }; int Test::var = 42; ``` D ```cpp extern(C++, class) struct Test { extern (C++) export extern __gshared int var; } ``` (also tried without the duplicate extern

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 21:30:47 UTC, Tim wrote: It seems to work if var is additionally static: Ha, it works indeed! ```cpp extern export __gshared static int var; ``` That's a declaration worthy of C++ ! Fewer keywords would be too usable :D Joking aside, I understood the docs

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 15, 2021 at 10:01:19PM +, Jan via Digitalmars-d-learn wrote: > On Wednesday, 15 December 2021 at 21:30:47 UTC, Tim wrote: [...] > ```cpp > extern export __gshared static int var; > ``` [...] > Joking aside, I understood the docs such that `__gshared` actually > *is* `static` in D, n

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Adam Ruppe via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:24:42 UTC, H. S. Teoh wrote: `__gshared` is needed to coax the compiler into making the variable global in the C/C++ sense, i.e., only 1 instance across all threads. it is just normally __gshared implies static automatically. it does in like every other c

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Tim via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:01:19 UTC, Jan wrote: Ha, it works indeed! ```cpp extern export __gshared static int var; ``` That's a declaration worthy of C++ ! Fewer keywords would be too usable :D Joking aside, I understood the docs such that `__gshared` actually *is* `static` in D

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:33:15 UTC, Tim wrote: I agree that __gshared should imply static. That's probably a bug in the compiler. Using `extern` without `export` would only work with static linking (on Windows). `export` without `extern` would be exporting the variable for others,

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Tim via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:46:01 UTC, Jan wrote: Btw. should I report this issue somewhere? Is far as I see this isn't logged yet: Yes, it should be reported.

Re: How to pass a class by (const) reference to C++

2021-12-15 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:50:38 UTC, Tim wrote: On Wednesday, 15 December 2021 at 22:46:01 UTC, Jan wrote: Btw. should I report this issue somewhere? Is far as I see this isn't logged yet: Yes, it should b

Re: How to pass a class by (const) reference to C++

2021-12-16 Thread Jan via Digitalmars-d-learn
On Wednesday, 15 December 2021 at 22:50:38 UTC, Tim wrote: On Wednesday, 15 December 2021 at 22:46:01 UTC, Jan wrote: Btw. should I report this issue somewhere? Is far as I see this isn't logged yet: Yes, it should b

Re: How to pass a class by (const) reference to C++

2021-12-16 Thread Tim via Digitalmars-d-learn
On Thursday, 16 December 2021 at 08:30:14 UTC, Jan wrote: Ok, next problem. Maybe you know the necessary syntax for this one too. C++ ```cpp class Test { public: __declspec(dllexport) static const int const_vars[2]; __declspec(dllexport) static int nonconst_vars[2]; }; const int Test::con

Re: How to pass a class by (const) reference to C++

2021-12-16 Thread Jan via Digitalmars-d-learn
On Thursday, 16 December 2021 at 16:21:30 UTC, Tim wrote: That looks like another bug in the compiler. pragma(mangle, ...) should work as a workaround. Another bug ticket it is then: https://issues.dlang.org/show_bug.cgi?id=22604