Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 01, 2014 D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
In the following pages: http://dlang.org/interfaceToC.html http://dlang.org/cpp_interface the "Data Type Compatibility" section says D int is compatible with C/C++ int. Isn't this actually false because D's integer types are fixed-size whereas C/C++'s are variable? So D int is only compatible with C++ int32_t, and who knows what that is typedef-ed to? Likewise for uint, long, short etc. So how to ensure, when calling C/C++, that the D int etc are being mapped to the correctly-sized C/C++ type? Does the compiler ensure that since the compatibility is being advertised as built-in? -- Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा |
November 01, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | D claims compatibility with system C compiler, which usually have 32-bit int. |
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
> D claims compatibility with system C compiler, which usually have 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | On Saturday, 1 November 2014 at 15:00:57 UTC, Shriramana Sharma via Digitalmars-d-learn wrote:
> In the following pages:
>
> http://dlang.org/interfaceToC.html
> http://dlang.org/cpp_interface
>
> the "Data Type Compatibility" section says D int is compatible with
> C/C++ int. Isn't this actually false because D's integer types are
> fixed-size whereas C/C++'s are variable? So D int is only compatible
> with C++ int32_t, and who knows what that is typedef-ed to? Likewise
> for uint, long, short etc.
>
> So how to ensure, when calling C/C++, that the D int etc are being
> mapped to the correctly-sized C/C++ type? Does the compiler ensure
> that since the compatibility is being advertised as built-in?
Note this at the end of the "Data Type Compatiblity" section:
"These equivalents hold for most C compilers. The C standard does not pin down the sizes of the types, so some care is needed."
D's built-in types do not change based on the companion C compiler. When linking to C, it is the programmers responsibility to ensure they are using the right types.
core.stdc.config can help deal with the most common inconsistencies.
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm@gmx.net>" wrote:
> On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
>> D claims compatibility with system C compiler, which usually have
>> 32-bit int.
>
> ... and for C/C++ long which can be 32 or 64 bit, DMD recently
> introduced the types c_long and c_ulong. (Not released yet.)
Those aren't new. They have been in core.stdc.config for quite some time.
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Sunday, 2 November 2014 at 12:37:13 UTC, Mike Parker wrote:
> On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm@gmx.net>" wrote:
>> On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
>>> D claims compatibility with system C compiler, which usually have
>>> 32-bit int.
>>
>> ... and for C/C++ long which can be 32 or 64 bit, DMD recently
>> introduced the types c_long and c_ulong. (Not released yet.)
>
> Those aren't new. They have been in core.stdc.config for quite some time.
Ah, I see. But DMD now has support for mangling these types correctly, in commit c4e0de64.
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 2 November 2014 at 11:59:27 UTC, Marc Schütz wrote:
> On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
>> D claims compatibility with system C compiler, which usually have 32-bit int.
>
> ... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
c_long and c_ulong have existed as aliases in core.stdc since the beginning. The change was to make them an explicit type so name mangling could be different.
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Sunday, 2 November 2014 at 12:37:13 UTC, Mike Parker wrote:
> On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm@gmx.net>" wrote:
>> On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
>>> D claims compatibility with system C compiler, which usually have
>>> 32-bit int.
>>
>> ... and for C/C++ long which can be 32 or 64 bit, DMD recently
>> introduced the types c_long and c_ulong. (Not released yet.)
>
> Those aren't new. They have been in core.stdc.config for quite some time.
c_long and c_ulong get used, should c_int and c_uint too in bindings?
Looks like fringe use case.
|
November 02, 2014 Re: D int and C/C++ int etc not really compatible when interfacing to C/C++ | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | On Sunday, 2 November 2014 at 16:53:06 UTC, ponce wrote:
>
> c_long and c_ulong get used, should c_int and c_uint too in bindings?
> Looks like fringe use case.
On common 32 and 64-bit platforms, the only type whose size changed between 32 and 64 bits is long, so the other aliases were deemed unnecessary. It's possible that as D is ported to more platforms this will have to change, but I seriously hope not.
|
Copyright © 1999-2021 by the D Language Foundation