Thread overview
extern(C) bool function type?
Sep 26, 2013
Charles Hixson
Sep 26, 2013
bearophile
Sep 26, 2013
Charles Hixson
Sep 26, 2013
bearophile
Sep 27, 2013
Charles Hixson
Sep 26, 2013
Jacob Carlborg
September 26, 2013
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?

-- 
Charles Hixson

September 26, 2013
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
September 26, 2013
On 2013-09-26 20:56, Charles Hixson wrote:
> 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?

In general 0 is false and all other integers are true. So if the function returns 0 or 1 it will automatically work for true and false as well.

-- 
/Jacob Carlborg
September 26, 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

September 26, 2013
Charles Hixson:

> 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.)

Take a look at this entertaining page:
http://dlang.org/phobos/std_stdint.html

Bye,
bearophile
September 27, 2013
I'd forgotten about:

	int_least8_t

I guess that's what I should use.

On 09/26/2013 03:06 PM, bearophile wrote:
> Charles Hixson:
>
>> 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.)
>
> Take a look at this entertaining page: http://dlang.org/phobos/std_stdint.html
>
> Bye,
> bearophile
>


-- 
Charles Hixson