digitalmars.D.learn - C++ and D bool compatibility
- Jeremy DeHaan (9/9) Apr 27 2013 Hey guys!
- bearophile (9/15) Apr 27 2013 If it misses bool, then maybe that page needs a documentation
- Jeremy DeHaan (53/69) May 02 2013 Well I realized that I was being dumb in the sense that I am
- bearophile (7/9) May 02 2013 D bools are 1 byte, but C chars don't need to be 1 byte, so you
- Jeremy DeHaan (9/16) May 02 2013 Technically speaking, you are right. Generally speaking, it's
Hey guys! I was reading this: http://dlang.org/cpp_interface.html And it mentions the various types and their compatibility with one another, but it leaves out bools. It would be very useful for me if it works out like this, but does anyone know off the top of their heads/tried it before? Thanks in advance! Jeremy
Apr 27 2013
Jeremy DeHaan:I was reading this: http://dlang.org/cpp_interface.html And it mentions the various types and their compatibility with one another, but it leaves out bools. It would be very useful for me if it works out like this, but does anyone know off the top of their heads/tried it before?If it misses bool, then maybe that page needs a documentation patch. bools in D are represented with 1 byte, 0 or 1. In C++ their size is not fixed: "for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1." Bye, bearophile
Apr 27 2013
On Saturday, 27 April 2013 at 23:38:17 UTC, bearophile wrote:Jeremy DeHaan:Well I realized that I was being dumb in the sense that I am mainly using a C interface and not really a C++ interface. That said, after playing with some ideas, I have something that works, though I am not entirely sure how safe it is. Consider the following, In C/C++.... DBoolTest.h: typedef char DBool; #define DTrue 1; #define DFalse 0; SOME_EXPORT_MACRO DBool isAGreaterThanB(int a, int b);//this is an extern "C" dll export macro DBoolTest.cpp: #include "DBoolTest.h" DBool isAGreaterThanB(int a, int b) { if(a>b) { return DTrue; } else { return DFalse; } } In D... main.d: module main; import std.stdio; void main(string[] args) { writeln("Is 1 greater than 2?"); writeln(isAGreaterThanB(1,2)); // Lets the user press <Return> before program returns stdin.readln(); } extern(C) { bool isAGreaterThanB(int a, int b); } Linking the D program with the C/C++ DLL and running main.exe results in the output: Is 1 greater than 2? false D bools are 1 byte, and C/C++ chars are 1 byte as well and it works. How safe is it to mix types like this between the two languages though? I would rather have a nice clean solution than to put together something that is considered "hacky." That said, if something like this isn't a big deal, it would make my D code more straight forward and a little cleaner. Any thoughts on this guys? JeremyI was reading this: http://dlang.org/cpp_interface.html And it mentions the various types and their compatibility with one another, but it leaves out bools. It would be very useful for me if it works out like this, but does anyone know off the top of their heads/tried it before?If it misses bool, then maybe that page needs a documentation patch. bools in D are represented with 1 byte, 0 or 1. In C++ their size is not fixed: "for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1." Bye, bearophile
May 02 2013
Jeremy DeHaan:D bools are 1 byte, and C/C++ chars are 1 byte as well and it works.D bools are 1 byte, but C chars don't need to be 1 byte, so you are working with an implementation detail. I think in C99+ it's better to use uint8_t from stdint.h, that's safely always 1 byte long. Bye, bearophile
May 02 2013
On Friday, 3 May 2013 at 01:03:39 UTC, bearophile wrote:Jeremy DeHaan:Technically speaking, you are right. Generally speaking, it's probably going to be pretty rare for a compiler these days to define a char type as more the 8 bits so I figured I would be safe.D bools are 1 byte, and C/C++ chars are 1 byte as well and it works.D bools are 1 byte, but C chars don't need to be 1 byte, so you are working with an implementation detail.I think in C99+ it's better to use uint8_t from stdint.h, that's safely always 1 byte long.I agree with you on this though, and it is definitely a better solution. Question though, is there any difference between uint_t and int8_t for this kind of purpose? They are the same size, but the former is just unsigned.
May 02 2013