octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mxIs* functions now return bool


From: John W. Eaton
Subject: Re: mxIs* functions now return bool
Date: Sun, 11 Sep 2016 11:07:33 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.2.0

On 09/11/2016 10:56 AM, Rik wrote:
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

OK, I remember now that there was a patch submitted for this. The initial change was:

  http://hg.savannah.gnu.org/hgweb/octave/rev/ef3e0408097b

but that wasn't quite right and there were a couple of follow-up changesets. See also

  https://savannah.gnu.org/patch/?8526

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.

I'm not sure either.

It should work now either way so maybe it's not necessary to change anything? OTOH, just using stdbool.h seems like the right solution since any system that can run modern Octave should have a compiler that provides stdbool.h.

jwe




reply via email to

[Prev in Thread] Current Thread [Next in Thread]