extern(C) bool function type?
Charles Hixson
charleshixsn at earthlink.net
Thu Sep 26 14:40:16 PDT 2013
On 09/26/2013 12:19 PM, bearophile wrote:
> Charles Hixson:
>
>> Does anyone know:
>> If an extern(c) function is defined as returning a bool, does D
>> handle the conversion automatically into true and false? Should it
>> be defined as returning an int? On a 64 bit machine? Does this
>> depend on the compiler used to compile the library? int32_t? int_64_t?
>>
>> Is this portable or non-portable?
>
> A D bool is a C99 uint8_t that has values just 0 or 1.
>
> If your uint8_t contains a value x > 1, in many cases this works, but
> some D code relying on the standard values of a D boolean breaks (like
> when you sum bool values in D, to count the true ones).
>
> D doesn't handle those conversions beside the narrowing or extension
> of bit-width lengths.
>
> Bye,
> bearophile
>
It's a library function, that claims to return bool, but is a C
function. My suspicion is that the proper way to handle this is to say
that it returns an int, or possibly an int32_t, i.e. to declare is as
not returning a bool, even though the library says that that's what it
returns. But my computer is a 64 bit machine, and the code was compiled
awhile ago, so I suspect it may really be returning an int32_t.
The problem is that if I guess wrong this will not dependably cause a
problem. (I had a hope that D would say "O, it's a C routine returning
a bool, so I know how to deal with that", but that wasn't really my
expectation.)
Yeah, if I were defining the function, I'd define it in a way that I
know how to handle properly. But I'm not, and the interface is for
either C or C++. I can choose between those two.
So... if C returns an int64_t and I declare it as an int32_t, will this
cause problems? What about if C returns an int32_t and I declare it as
an int64_t? Or *is* there a good way to handle this? (I think that
there's an error code I could interrogate if I need to just ignore the
result...but I don't know if I can do this in all cases.)
--
Charles Hixson
More information about the Digitalmars-d-learn
mailing list