3

The Arduino documentation says if(Serial) always returns 1, except for the case where Leonardo USB serial connection delays becoming ready.

(Leonardo has two USB IDs, and switches between them for boot loader and Serial USB. After boot loader finishes and kicks off the program, it takes a while for the USB master (e.g. your PC) to process the disconnect of the boot loader USB device and the connection of the Serial USB device. Thus the need for a wait.)

Now "Serial" is a static external instantiation of the Serial_ class (defined as extern Serial_ Serial;).

The c++ rules, as I understand them, do not allow a constructor to return a value, nor may the class name (which is also the constructor name) be used as a member (function) name in the class.

"Serial" cannot be defined in class Serial_, or the if(Serial) syntax would have to be if(Serial.Serial).

"Serial" cannot be defined outside the class Serial_, or it will conflict with (i.e. hide) the definition of Serial as an instantiation of Serial_.

So how does the Arduino IDE do it?

All I can think of is that the magic pre-scanner does something... But what exactly, pray tell?

  • BTW, I switched over to [link]drazzy.com/package_drazzy.com_index.json[/link] Spence Konde core and fixed my compile problems. Sometimes it's a real pain to figure out what is the latest/greatest most evolved / currently developed package to use. – HiTechHiTouch Aug 10 '16 at 10:14
  • Serial isn't the class name, its the instance name. – Code Gorilla Aug 17 '16 at 7:45
  • @Matt, while what you say is true, where is it misused in the question? Understand that I can't fix the case in the title (StackExchange edit restriction), and that if(serial) is a quote from the Arduino people, who are trying to dumb things down. I did say Serial is the instance of Serial_. So, pray tell, what other words need correction? – HiTechHiTouch Aug 18 '16 at 14:42
3

It's not the constructor that's returning the value, it's the bool operator:

Uno:

HardwareSerial::operator bool() {
    return true;
}

Leonardo:

Serial_::operator bool() {
    bool result = false;
    if (_usbLineInfo.lineState > 0)
        result = true;
    delay(10);
    return result;
}

When you use the object in a setting where a boolean value is expected of it (e.g., if, while etc.) the bool() operator is called (if it exists) and that value is used.

More specifically, the bool() operator is called when the object is being cast to a bool type, either implicitly (as in if...) or explicitly (as in ... = (bool)Serial;).

More on C++ class operators here: http://en.cppreference.com/w/cpp/language/operators

  • Thanks @Majenjo. C++ has been inactive quite a while for me, and now that you point it out, I'm doing "head slaps" over how obvious it is! – HiTechHiTouch Aug 10 '16 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.