www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - extern(C++): override nonvirtual member function

reply sfp <sfp hush.ai> writes:
I'm trying to wrap some C++ classes, and one issue I've run into 
is having an extern(C++) class inheriting from another, with the 
child doing a "nonvirtual override" of a nonvirtual member 
function in the base class... E.g., in C++:
```
struct A {
   void f() { ... }
   ...
   // some other virtual functions
};

struct B {
   void f() { ... } // nonvirtual override A::f
   ...
   // other VFs
};
```
D doesn't seem to like this very much... Is there some way to 
hack around this issue? For a variety of reasons, I can't make 
any modifications to the C++ code I'm wrapping.

For more context, I'm writing a script that's generating wrappers 
for about 150 header files, maybe 100-200k LOC (using the 
cxxheaderparse Python library). This is actually going pretty 
well, but he best solution I can come up with right now is to try 
to detect cases like this and omit them, but it's less than 
ideal. The C++ library is mostly C++98 style and it's been smooth 
sailing with the occasional wart I've needed to figure out like 
this.
Apr 24
parent reply evilrat <evilrat666 gmail.com> writes:
On Thursday, 24 April 2025 at 19:41:48 UTC, sfp wrote:
 I'm trying to wrap some C++ classes, and one issue I've run 
 into is having an extern(C++) class inheriting from another, 
 with the child doing a "nonvirtual override" of a nonvirtual 
 member function in the base class... E.g., in C++:
 ```
 struct A {
   void f() { ... }
   ...
   // some other virtual functions
 };

 struct B {
   void f() { ... } // nonvirtual override A::f
   ...
   // other VFs
 };
 ```
 D doesn't seem to like this very much... Is there some way to 
 hack around this issue? For a variety of reasons, I can't make 
 any modifications to the C++ code I'm wrapping.

 For more context, I'm writing a script that's generating 
 wrappers for about 150 header files, maybe 100-200k LOC (using 
 the cxxheaderparse Python library). This is actually going 
 pretty well, but he best solution I can come up with right now 
 is to try to detect cases like this and omit them, but it's 
 less than ideal. The C++ library is mostly C++98 style and it's 
 been smooth sailing with the occasional wart I've needed to 
 figure out like this.
both f() would be marked `final` in D which will tell it is 'nonvirtual override', and then you will have more headache. check this example on how C++ behavior depends on how it was laid out in code... https://godbolt.org/z/qojxM5Tj7
Apr 24
parent reply sfp <sfp hush.ai> writes:
On Friday, 25 April 2025 at 05:24:55 UTC, evilrat wrote:
 both f() would be marked `final` in D which will tell it is 
 'nonvirtual override', and then you will have more headache.
I don't care if I have a headache or not, I need to wrap the C++ code and I can't change it. Needs must. I tried this already. E.g.: ``` extern(C++) class A { final void f() {} void g() {} // need at least one virtual function... } extern(C++) class B: A { final void f() {} } void main() {} ``` and dmd complains about it: ``` Error: function `main.B.f` cannot override `final` function `main.A.f` ```
Apr 25
parent reply evilrat <evilrat666 gmail.com> writes:
On Friday, 25 April 2025 at 16:59:10 UTC, sfp wrote:
 On Friday, 25 April 2025 at 05:24:55 UTC, evilrat wrote:
 both f() would be marked `final` in D which will tell it is 
 'nonvirtual override', and then you will have more headache.
I don't care if I have a headache or not, I need to wrap the C++ code and I can't change it. Needs must.
but you already having it :)
 I tried this already. E.g.:
 ```
 extern(C++) class A {
   final void f() {}

   void g() {} // need at least one virtual function...
 }

 extern(C++) class B: A {
   final void f() {}
 }

 void main() {}
 ```
 and dmd complains about it:
 ```
 Error: function `main.B.f` cannot override `final` function 
 `main.A.f`
 ```
Well then, time to resolve to some craftery. you can do alias and use pragma mangle to fix mangling. enjoy. ```d import std.string; import std.conv : text; string fixMangle(alias sym, string name)() { version(Windows) { return sym.mangleof.replace(__traits(identifier, sym), "f"); } else version(Posix) { return sym.mangleof.replace(__traits(identifier, sym).length.text ~ __traits(identifier, sym), name.length.text ~ name ); } else static assert (0, "unsupported system"); } extern(C++, struct) class A { // by default will look like this on windows "?f_A A QEAAX" // can be fixed with the help of pragma mangle & replace //pragma(mangle, f.mangleof.replace("f_A", "f")) // but here is more system agnostic way pragma(mangle, fixMangle!(f_A, "f")) final void f_A() {} alias f = f_A; final void a(); void g() { f(); } // need at least one virtual function... } // defined as f_A but now will be properly reflected as "?f A QEAAX" // or for linux target `ldc2 foo.d -mtriple=x86_64-linux-unknown-unknown -c` "_ZN1A1fEv" (here it is named 1f) pragma(msg, A.f.mangleof); pragma(msg, B.f.mangleof); extern(C++, struct) class B: A { pragma(mangle, f.mangleof.replace("f_B", "f")) final void f_B() {} alias f = f_B; override void g() { f(); } } void main() {} ```
Apr 25
parent sfp <sfp hush.ai> writes:
On Friday, 25 April 2025 at 18:01:25 UTC, evilrat wrote:
 On Friday, 25 April 2025 at 16:59:10 UTC, sfp wrote:
 [...]
but you already having it :)
 [...]
Well then, time to resolve to some craftery. [...]
I'll see how this goes... Looks promising. Thanks for the aspirin. :-)
Apr 25