www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ interop - class vs struct

reply Walter Bright <newshound2 digitalmars.com> writes:
C++ name mangling distinguishes between a class and a struct. This distinction 
has no semantic meaning, but there it is in the name mangling. In order to 
interop, we have to have a way to tell D to mangle a struct as a 'class' when 
interfacing with C++.

I have some ideas, but they're all kinda ugly.

Any ideas?
Sep 10 2014
next sibling parent "FreeSlave" <freeslave93 gmail.com> writes:
On Wednesday, 10 September 2014 at 20:43:44 UTC, Walter Bright 
wrote:
 C++ name mangling distinguishes between a class and a struct. 
 This distinction has no semantic meaning, but there it is in 
 the name mangling. In order to interop, we have to have a way 
 to tell D to mangle a struct as a 'class' when interfacing with 
 C++.

 I have some ideas, but they're all kinda ugly.

 Any ideas?
Just for clarification. I should notice it relates only to msvc mangling model, which does not respect equivalence of struct and class and use different symbols to mangle them (U and V). There is also warning C4099 because of it. g++ seems to use same mangling regardless of how you declare class (with class or struct keyword). Even if you find good solution by extending language (for example, by adding extern(C++-struct)) it would make sense only for Windows platforms.
Sep 10 2014
prev sibling parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Walter Bright"  wrote in message news:luqd60$2u0r$1 digitalmars.com...

 C++ name mangling distinguishes between a class and a struct. This
 distinction has no semantic meaning, but there it is in the name mangling. 
 In order to
 interop, we have to have a way to tell D to mangle a struct as a 'class' 
 when interfacing with C++.

 I have some ideas, but they're all kinda ugly.

 Any ideas?
pragma(cpp_mangle_as_struct) class Foo { } This is a low-level feature, and should be ugly.
Sep 10 2014
next sibling parent reply Lionello Lunesu <lionello lunesu.remove.com> writes:
On 11/09/14 14:32, Daniel Murphy wrote:
 pragma(cpp_mangle_as_struct)
 class Foo
 {
 }
and/or? pragma(cpp_mangle_as_class) struct Bar { }
Sep 11 2014
parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Lionello Lunesu"  wrote in message news:luriec$1081$1 digitalmars.com...

 and/or?

 pragma(cpp_mangle_as_class)
 struct Bar
 {
 }
Yeah. No need to add new syntax, and it means adding a way to make D link with C++ rather than adding C++ features to D.
Sep 11 2014
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 9/10/14, 11:32 PM, Daniel Murphy wrote:
 "Walter Bright"  wrote in message news:luqd60$2u0r$1 digitalmars.com...

 C++ name mangling distinguishes between a class and a struct. This
 distinction has no semantic meaning, but there it is in the name
 mangling. In order to
 interop, we have to have a way to tell D to mangle a struct as a
 'class' when interfacing with C++.

 I have some ideas, but they're all kinda ugly.

 Any ideas?
pragma(cpp_mangle_as_struct) class Foo { } This is a low-level feature, and should be ugly.
I think attributes would be the go-to approach. -- Andrei
Sep 11 2014
parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Andrei Alexandrescu"  wrote in message 
news:lurk9p$123f$1 digitalmars.com...

 I think attributes would be the go-to approach. -- Andrei
Why?
Sep 11 2014
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 9/11/14, 12:57 AM, Daniel Murphy wrote:
 "Andrei Alexandrescu"  wrote in message
 news:lurk9p$123f$1 digitalmars.com...

 I think attributes would be the go-to approach. -- Andrei
Why?
Because they postdate pragma :o). I'm fine either way. -- Andrei
Sep 11 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 11 September 2014 at 07:58:14 UTC, Andrei
Alexandrescu wrote:
 On 9/11/14, 12:57 AM, Daniel Murphy wrote:
 "Andrei Alexandrescu"  wrote in message
 news:lurk9p$123f$1 digitalmars.com...

 I think attributes would be the go-to approach. -- Andrei
Why?
Because they postdate pragma :o). I'm fine either way. -- Andrei
Those are different tools. Attributes are part of language semantics, pragmas are to work with compiler internals. It is not like we have `getPragmas` trait. I agree that this is better fit for a pragma.
Sep 11 2014
parent "Idan Arye" <GenericNPC gmail.com> writes:
On Thursday, 11 September 2014 at 09:09:41 UTC, Dicebot wrote:
 On Thursday, 11 September 2014 at 07:58:14 UTC, Andrei
 Alexandrescu wrote:
 On 9/11/14, 12:57 AM, Daniel Murphy wrote:
 "Andrei Alexandrescu"  wrote in message
 news:lurk9p$123f$1 digitalmars.com...

 I think attributes would be the go-to approach. -- Andrei
Why?
Because they postdate pragma :o). I'm fine either way. -- Andrei
Those are different tools. Attributes are part of language semantics, pragmas are to work with compiler internals. It is not like we have `getPragmas` trait. I agree that this is better fit for a pragma.
Also, there already is a pragma for controlling the mangling, so it'll be easier to extend it. Something like: extern (C++) pragma(mangle, struct) interface E { int bar(int i, int j, int k); }
Sep 11 2014