Thread overview
extern(C++): override nonvirtual member function
Apr 24
sfp
Apr 25
evilrat
Apr 25
sfp
Apr 25
evilrat
Apr 25
sfp
April 24

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.

April 25

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

April 25

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`
April 25

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.

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() {}
April 25

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. :-)