vendredi 16 août 2019

Is nullptr falsy? [duplicate]

This question already has an answer here:

When used as a boolean expression or transformed into a boolean either explicitly or implicitly, is nullptr consistently false? Is this implementation defined or specified in the standard?

I wrote some code to test, but am not certain if it tests this property fully. I couldn't find an existing SO answer that talked specifically about this. cppreference doesn't mention this from what I see.

if (nullptr) {
    ;
} else {
    std::cout << "Evaluates to false implicitly\n";
}

if (!nullptr) {
    std::cout << "Evaluates to false if operated on\n";
}

if (!(bool)(nullptr)) {
    std::cout << "Evaluates to false if explicitly cast to bool\n";
}

Expected and actual:

Evaluates to false implicitly
Evaluates to false if operated on
Evaluates to false if explicitly cast to bool

Aucun commentaire:

Enregistrer un commentaire