Published using Google Docs
GDAL RFC - int bFoo -> bool bFoo (simple case)
Updated automatically every 5 minutes

RFC: int bFoo -> bool bFoo for local vars - simplest case

Author: Kurt Schwehr schwehr@google.com / schwehr@gmail.com (osgeo id: goatbar)

Started: 2016-May-09

Status: Open for discussion

Discussion: https://lists.osgeo.org/pipermail/gdal-dev/2016-June/044511.html 

Link: https://goo.gl/hdzhXD Google doc link

RFC: int bFoo -> bool bFoo for local vars - simplest case

Notes to readers for context:

Consider:

Drawbacks:

Advantages:

Proposed solution:

Benefits:

Drawbacks:

Gotchya / boobytraps:

Cases not covered:

Alternatives:

Code samples from GDAL and autotest2:

See also:

Other RFCs like this one for reference:

Consider:

     int bFoo = FALSE;

     int bBar = expThatIsFalse || expThatIsTrue;

     int bBaz = CPLReturnsIntTrueFalse();

     int bHadRuntimeTrouble = -1;

     bHadRuntimeTrouble = expression;

Drawbacks:

Advantages:

Proposed solution:

     bool bFoo = false;

     bool bBar = expThatIsFalse || expThatIsTrue;

     bool bBaz = CPL_TO_BOOL(CPLReturnsIntTrueFalse());

     bool bHadRuntimeTrouble = false;

     bool bCheckedHadRuntimeTrouble = false;

     …

     bHadRuntimeTrouble = expression;

     bCheckedHadRuntimeTrouble = true;

Benefits:

 

Drawbacks:

Gotchya / boobytraps:

Cases not covered:

Alternatives:

For bool bFoo = false;

Converting from int to bool with CPL_TO_BOOL:

For int bHadRuntimeTrouble = -1;

Scale of change:

Medium

In most of the cases in GDAL, the behavior will be exactly the same.  The old convention occurs throughout the code base, but is really pretty simple.  There will be no externally visible changes except for possible performance changes (which should almost always be faster).  It is possible that a rare case could make it harder for a compiler to cleanly layout memory, but those cases should only occur with changes from int to bool that are excluded from this RFC.  In general, this should make compilers, analyzers and debuggers have an easier time working with the code and resulting binary objects.  Memory usage may decrease very slightly.

Code samples from GDAL and autotest2:

Not all of these uses of TRUE and FALSE can be converted to true and false, but most of them can.

find . -name \*.cpp | xargs egrep 'TRUE|FALSE'  | wc -l

   14335

find . -name \*.cpp | xargs egrep 'true|false'  | wc -l

    6563

Notes to readers for context:

http://www.possibility.com/Cpp/CppCodingStandard.html#boolean  When was this true?

The new C++ standard defines a native boolean type. Until all compilers support bool, and existing code is changed to use it, we must still deal with the cruel world.

This must be from long long ago.  All compilers that will build GDAL support the C++ bool type.

See also:

Other RFCs like this one for reference: