Skip to content

Arduino.h boolean type #5440

Closed
Closed
@mcspr

Description

@mcspr

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

Settings in IDE

  • Module: Generic ESP8266 Module

Problem Description

I had noticed that the boolean is typedefed to uint8_t, not bool as one would think:

typedef uint8_t boolean;

Arduino base repo had an issue about this problem: arduino/Arduino#2147

Gist of the original issue is that boolean & bool comparison through == may yield different than expected results. Just like this wiki article describes benefits of having native bool vs. implementing it through int: https://en.wikipedia.org/wiki/Boolean_data_type#C,_C++,_Objective-C,_AWK

The language guarantees that any two true values will compare equal (which was impossible to achieve before the introduction of the type).

AVR, SAMD etc. Cores have it typedef'd to bool since. After that, arduino/Arduino#4673 was raised about replacing boolean with bool all throughout the Cores.

So my questions are:

  • will PR simply replacing uint8_t -> bool in Arduino.h break something here?
  • should boolean used here (examples, core etc.) be replaced by bool?

MCVE Sketch

From the issue mentioned above, slightly modified for verbosity.
Conditions resolve the same ofc.

#include <Arduino.h>

#define CHECK(condition) Serial.print(#condition " is "); Serial.println((condition) ? "true" : "false");

void setup() {
    Serial.begin(115200);
    CHECK(true == (bool)57);
    CHECK(true == (boolean)57);
    CHECK(true == bool( true ));
    CHECK(bool( 1 | 2 | 4 | 8 ) == boolean( 1 | 2 | 4 | 8 ));
    CHECK(true == false + 1);
    CHECK(true + true == true);
    CHECK(bool( true + true ) == true);
    CHECK(boolean( true + true ) == true);
}
void loop() {}

Debug Messages

true == (bool)57 is true
true == (boolean)57 is false
true == bool( true ) is true
bool( 1 | 2 | 4 | 8 ) == boolean( 1 | 2 | 4 | 8 ) is false
true == false + 1 is true
true + true == true is false
bool( true + true ) == true is true
boolean( true + true ) == true is false

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions