4 ms·
How would that even work? Modern C++ libraries are largely templates, templates which must be instantiated at compile-time i.e. you need a full C++ compiler to
by copx 9y ago
How would that even work? Modern C++ libraries are largely templates, templates which must be instantiated at compile-time i.e. you need a full C++ compiler to use C++ libraries directly.
Said compiler must somehow work together with the D compiler here, which should lead to god-awful memory use, compilation time, and error messages.
- _yosefk 9y agoMost useful C++ code is either not templates or it's templates which can be instantiated a finite number of times and then used like non-templated code. Most "header-only"/header-heavy C++ libraries which present the problem you mention solve problems like C++'s smart for loop working like "for x in xs" does in Python but not working like "for x,y in zip(xs,ys)" does or like "for i,x for enumerate(xs)" does etc.; you don't really need these outside C++ (whether you need them inside C++ is another question but it's irrelevant here.)
- CJefferson 9y agoThat sounds ..wrong. what about the C++ standard library? Neither vector or sort sounds like that?
- yorwba 9y agoThose > can be instantiated a finite number of times and then used like non-templated code. So you have a C++ file that uses vector<int>, vector<struct c_struct>, vector<CppClass>, vector<DClass>, etc. once and then you can link to them from D code. The only case where that doesn't work is when you have lots of one-off instantiations, where requiring an instantiation in C++ code for each use in D would get annoying quickly.
- amelius 9y agoDoes the D compiler know how to mangle/demangle C++ names? Do you have to write some kind of D header file for foreign C++ functions? Otherwise, how would D even know about the existence of e.g. vector<int>::push?
- biotronic 9y agohttps://dlang.org/spec/cpp_interface.html#cpp-templates https://dlang.org/spec/cpp_interface.html#cpp-templates You will need to recreate the C++ function definitions in D and mark them extern(C++), but that's about it.
- pzone 9y agoThose parts of the C++ standard library are header-only and will be instantiated with concrete types at compile-time.
- penpapersw 9y agoI assume the idea is to be able to reuse libraries like Box2d etc where it's mostly concrete code, and that other libraries where it's mostly templates kind of just have to be rewritten.
- jjnoakes 9y agohttps://dlang.org/spec/cpp_interface.html https://dlang.org/spec/cpp_interface.html
- seren 9y ago> D understands how C++ function names are "mangled" and the correct C++ function call/return sequence. How does it work in practice ? C++ name mangling is not standardized and so every compiler can implement its own scheme. So does it mean there is a list of D compatible c++ compiler somewhere ? I have the feeling that if you are using some exotic proprietary c++ compiler it won't work well.
- kryptiskt 9y agoIn practice there are two dominating standards nowadays, the Itanium ABI and Microsoft's, so it's quite tractable to handle the common cases.
- seren 9y agoThanks, I might be biased because I have a legacy platform still using gcc 2.7, which is older than the adoption of the Itanium ABI.
- kbenson 9y agoThere was some interesting discussion regarding this here recently in the comments regarding a Rust demangling library[1]. 1: https://news.ycombinator.com/item?id=13706799 https://news.ycombinator.com/item?id=13706799
- WalterBright 9y ago> C++ name mangling is not standardized and so every compiler can implement its own scheme. That's right, and the D compiler deals with that by customizing the C++ mangling for every platform.
- seren 9y ago
- iamNumber4 9y agoYou should probably read up on ABI Compatibility, and, Compiling vs linking as well. Let's be clear a compiler builds a object files that have to be linked to create a executable binary. IE compiler makes .o files. Linker takes output object files from the compiler an puts them (.o,.so/.dll,.a) together, to make .exe files. This all works because you are linking to the C/C++ library binaries (the .o, .so, .a, .dll) files generated by a C/C++ compiling/linking. In most cases this is shared object library/dll provided by the operating system or pre-built library you install on the system. There is no need to compile it because it is pre-built. if you do compile the library yourself, it is a one time thing. In D you create a .d file that describes what is in a C++ library .o/.so/.a/.dll file. So that upon linking, you take your D code's .o files + the C/C++ .o files to put together a full executable binary.
- johncolanduoni 9y agoThat's not really relevant to their question. Many templates are implemented via inlining/instantiation in client code by default, and as such they won't be present in the output of the C++ compiler.
- danieldk 9y agoI think you misunderstood your parent. A lot of modern C++ 'libraries' are header-only, because they heavily use templates. There is no object code or library to link against until you instantiate templates.
- enzo1982 9y agoThat's not how C++ templates work. You cannot deliver them as binaries. Templates are delivered as C++ include files (i.e. source code). When you use them, the compiler fills in the types you want and compiles the result. For example, there's the std::vector<> template. You can fill in any type that meets the specific requirements of the template and use it like std::vector<int> or std::vector<YourC++ClassType>. However, you probably won't be able to do std::vector<YourDClassType>. ABI compatibility is not enough to do this as you would be effectively mixing C++ and D source code here.
- 9y ago
- qznc 9y agoYou can port some of the templates to D, since it also has templates. Although I'm not sure if they work the same in all pathological cases.
- Zardoz84 9y agoany thing that you can do with C++ templates, could be done with D templates + CTFE plus would be far more easy to read and understand that C++ templates.
- mhh__ 9y agoNo it does not. C++ templates mean matching the name mangling. That already works. Catching C++ exceptions is being worked on now.
- WalterBright 9y agoYes, it is amazing that D can work with C++ templates! I gave a talk on how it is done here: https://www.youtube.com/watch?v=IkwaV6k6BmM https://www.youtube.com/watch?v=IkwaV6k6BmM