+1 vote
in D Programming by
Why Doesn't D Have An Interface To C++ As Well As C?

1 Answer

0 votes
by

Here are some reasons why it isn't a full interface:

Attempting to have D interface with C++ is nearly as complicated as writing a C++ compiler, which would destroy the goal of having D be a reasonably easy language to implement. For people with an existing C++ code base that they must work with, they are stuck with C++ (they can't move it to any other language, either).

There are many issues that would have to be resolved in order for D code to call some arbitrary C++ code that is presumed to be unmodifiable. This list certainly isn't complete, it's just to show the scope of the difficulties involved.

  • D source code is unicode, C++'s is ASCII with code pages. Or not. It's unspecified. This impacts the contents of string literals.
  • std::string cannot deal with multibyte UTF.
  • C++ has a tag name space. D does not. Some sort of renaming would have to happen.
  • C++ code often relies on compiler specific extensions.
  • C++ has namespaces. D has modules. There is no obvious mapping between the two.
  • C++ views source code as one gigantic file (after preprocessing). D sees source code as a hierarchy of modules and packages.
  • Enum name scoping rules behave differently.
  • C++ code, despite decades of attempts to replace macro features with inbuilt ones, relies more heavily than ever on layer after layer of arbitrary macros. D does not always have an analog for token pasting and stringizing.
  • Macro names have global scope across #include files, but are local to the gigantic source files.
  • C++ has arbitrary multiple inheritance and virtual base classes. D does not.
  • C++ does not distinguish between in, out and ref (i.e. inout) parameters.
  • The C++ name mangling varies from compiler to compiler.
  • C++ throws exceptions of arbitrary type, not just descendants of Object.
  • C++ overloads based on const and volatile. D overloads based on const and immutable.
  • C++ overloads operators in significantly different ways - for example, operator[]() overloading for lvalue and rvalue is based on const overloading and a proxy class.
  • C++ overloads operators like < completely independently of >.
  • C++ does not distinguish between a class and a struct object.
  • The vtbl[] location and layout is different between C++ and D.
  • The way RTTI is done is completely different. C++ has no classinfo.
  • D does not have two phase lookup, nor does it have Koenig (ADL) lookup.
  • C++ relates classes with the 'friend' system, D uses packages and modules.
  • C++ class design tends to revolve around explicit memory allocation issues, D's do not.
  • D's template system is very different.
  • C++ has 'exception specifications'.
  • C++ has global operator overloading.
  • C++ name mangling depends on const and volatile being type modifiers. D name mangling depends on const and immutable being type modifiers. D's const is also transitive, unlike C++. One cannot have a const pointer to mutable in D.

The bottom line is the language features affect the design of the code. C++ designs just don't fit with D. Even if you could find a way to automatically adapt between the two, the result will be about as enticing as the left side of a honda welded to the right side of a camaro.

Related questions

+1 vote
asked Mar 5, 2021 in D Programming by SakshiSharma
0 votes
asked Mar 5, 2021 in D Programming by SakshiSharma
...