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

Reply via email to