Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extern(C++) class with no virtual functions #17859

Open
dlangBugzillaToGithub opened this issue May 9, 2018 · 3 comments
Open

Extern(C++) class with no virtual functions #17859

dlangBugzillaToGithub opened this issue May 9, 2018 · 3 comments
Labels
Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code P4 Severity:Enhancement

Comments

@dlangBugzillaToGithub
Copy link

Manu reported this on 2018-05-09T21:10:50Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=18845

CC List

  • Ethan Watson
  • Mathias LANG (@Geod24)
  • Timothee Cour

Description

I just encountered a gotcha where D doesn't support extern(C++) classes with no vtable.

In C++, if a class has no virtual functions, there is no vtable.
If a virtual is added to a derived class, the vtable is pre-pended to the base.

Is it possible to replicate this in extern(C++)?
We have had a bunch of gotchas related to this.

You can argue that a base class with no virtual functions is a struct, and that may be true, except that its *intent* is that it shall be derived from. D can not derive a class from a struct, so the base-with-no-virtuals needs to be expressed as a class in D for the arrangement to translate and interact with D code.
@dlangBugzillaToGithub
Copy link
Author

gooberman commented on 2018-05-09T21:33:04Z

See: The giant workaround I had to do for Binderoo for this very thing in my DConf 2017 talk.

I believe the reason classes always have a vtable is so safe casting is still guaranteed. But that's just an assumption.

@dlangBugzillaToGithub
Copy link
Author

turkeyman commented on 2018-05-09T21:53:56Z

What does "safe casting" mean?
You mean that it might require pointer adjustment?

I mean, it's absolutely necessary that when casting extern(C++) classes, that some special logic is applied which may need to perform a pointer adjustment, just like when casting C++ classes in C++ ;)
We can't escape that. We either need to support it, or it's just broken.

At very least, we should emit an error when an extern(C++) class is declared with no virtual members saying it's not supported.

@dlangBugzillaToGithub
Copy link
Author

pro.mathias.lang (@Geod24) commented on 2019-06-14T06:54:46Z

*** Issue 18449 has been marked as a duplicate of this issue. ***

@thewilsonator thewilsonator added the Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code P4 Severity:Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants