[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: mxIs* functions now return bool
From: |
Rik |
Subject: |
Re: mxIs* functions now return bool |
Date: |
Sun, 11 Sep 2016 07:56:13 -0700 |
On 09/11/2016 07:36 AM, John W. Eaton wrote:
> On 09/11/2016 01:29 AM, Rik wrote:
>> 9/10/16
>> I've been working on improving the MEX interface in Octave. One of the
>> things I noticed is that the predicate tests in Matlab are all returning a
>> bool, whereas in Octave they return an int. A predicate test might be
>> something like mxIsScalar or mxIsComplex. Does anyone see a problem with
>> changing the function prototypes to return bool?
>>
>> extern OCTINTERP_API int mxIsComplex (const mxArray *ptr);
>> =>
>> extern OCTINTERP_API bool mxIsComplex (const mxArray *ptr);
>
> I suspect this is a relatively recent change in Matlab (for some value of
> relatively recent). I'm pretty sure they used to return int.
>
>> One of the things I don't understand is that the C language didn't have a
>> bool type until C99, and even then it was necessary to #include <stdbool.h>
>> in order to access it. However, I accidentally coded mxIsScalar to return
>> bool and it has compiled correctly. The only thing I see at the top of
>> mexproto.h is
>>
>> #if defined (__cplusplus)
>> # include <cstdlib>
>> extern "C" {
>> #else
>> # include <stdlib.h>
>> #endif
>>
>> When I run 'mex -v ...' I can see that the compiler is gcc, not g++, so I
>> don't think the first branch is being taken. Should I worry and add the
>> include for stdbool.h when compiling with a straight C compiler?
>
> If add -save-temps to the compiler command that is used and then look at
> the generated .i file can you see where bool is defined?
That was a help. It turns out that we are defining it ourselves in mex.h
#if ! defined (__cplusplus) && ! defined (__bool_true_false_are_defined)
# if ! defined (bool)
typedef int bool;
# endif
# if ! defined (true)
# define true 1
# endif
# if ! defined (false)
# define false 0
# endif
#endif
I'm not sure how many people are using a C compiler older than 17 years
(pre-C99), but it seems that there is a gnulib module for stdbool so I
think we can add that to the modules list and then unconditionally include
stdbool.h in the C side of Octave.
>
> If I compile the simple program "bool x;" with gcc, it errors on the
> undefined bool type. So I think there must be some place that bool is
> being defined.
>
> But I don't think it would hurt to include stdbool.h along with
> stdlib.h. If that causes trouble, I'm sure we'll hear about it.
Yes, and I thought if we're really going to change APIs we should do it on
a big change like the 4.2.0 release.
--Rik