Return values not reliable?

Hi,

When I check the return values from CVB functions it, seems that they sometimes succeed and still return FALSE.
E.g.

if (<CVB Function> == TRUE)
  // this does not happen even if CVB Function succeeds
  ...

After adding log messages to our code I see that not all CVB functions behave the same way. Some behave as excepted and evaluate to TRUE in the above code, some others don’t. Is this a bug? Any ideas?

Hi @DeprecatedCoder,

I would strongly advise against comparisons that follow the pattern

if (foo() == TRUE)
  ...

because checking versus a specific value is contrary to the concept behind boolean values.

In Common Vision Blox those functions returning boolean values return a value of type cvbbool_t which on the Windows platform is just a typedef for BOOL which itself is also just a typedef for int. One way or another the boolean types of programming languages always end up being mapped to integer types anyway…

Now functions returning a value of type BOOL are expected to either return TRUE or FALSE depending on the outcome, and with MSVC these two values have been defined as (see e.g. wtypes.h)

#define TRUE 1
#define FALSE 0

For a function compiled with MSVC and following the convention to either return TRUE or FALSE your approach to check versus TRUE would work well enough. Conceptually, however, the false case is to be associated with 0 and the true case with anything other than 0 and in fact a Delphi function with a signature like

Function foo() : LongBool;

returning True will return -1 if I recall correctly, and this is where the comparison versus TRUE will fail you.

Parts of Common Vision Blox have been implemented using Delphi compilers, and I assume you have run across one of these Delphi-based functions in your endeavors that causes your comparison to fail.

Luckily the solution is fairly easy:
Given a function

cvbbool_t foo();

Instead of writing

if (foo() == TRUE)

write

if (foo())

The latter will check successfully for any return value != 0 and fail for == 0 - exactly what you want in either case. This will make your code independent of the actual interpretation of TRUE but still follow its meaning, regardless how a given version of any given programming language implements TRUE.

2 Likes