////////////////// test.d ////////////////////////
extern(C++) class A
{
int f()
{
return 1;
}
}
extern(C++) class B: A
{
override int f() const
{
return 2;
}
}
extern(C++) B createB()
{
return new B;
}
////////////////// test.cpp //////////////////////
#include <stdio.h>
class A
{
public:
virtual int f();
};
class B: public A
{
public:
virtual int f() const;
};
B *createB();
int main()
{
const B *b = createB();
printf("%d\n", b->f());
return 0;
}
//////////////////////////////////////////////////
D allows to override the mutable function A.f with const B.f, because they are contravariant. The vtbl for B only contains one entry for f. C++ does not support that and expects a vtbl with two functions f. Calling b->f() results in a segmentation fault, because the vtbl entry for B.f does not exist.
There are probably also other differences for covariant/contravariant functions between D and C++.
Comment #1 by dlang-bot — 2022-05-25T12:33:54Z
@ibuclaw created dlang/dmd pull request #14169 "fix Issue 22351 - extern(C++) function contravariant in D, but not C++" fixing this issue:
- fix Issue 22351 - extern(C++) function contravariant in D, but not C++
https://github.com/dlang/dmd/pull/14169
Comment #2 by dlang-bot — 2022-05-27T01:15:46Z
dlang/dmd pull request #14169 "fix Issue 22351 - extern(C++) function contravariant in D, but not C++" was merged into master:
- 283b1ca4b54d42a9a22a9e2b34a045d69573f445 by Iain Buclaw:
fix Issue 22351 - extern(C++) function contravariant in D, but not C++
https://github.com/dlang/dmd/pull/14169