mercredi 3 avril 2019

C++ - Why is my else-statement in an if-else-statement ignored?

Well, I've come across a strange problem.

Since adding the spdlog library to my project and adjusting all log output accordingly, all else blocks of my if-else statements are ignored. The log output has been replaced by a search-and-replace procedure with a simple regex pattern (printf-style to fmtlib-style).

spdlog: https://github.com/gabime/spdlog

After some debugging, I have now written the following very simple test function:

void dbg_test() {
    bool success = false;

    BOOST_ASSERT_MSG(!success, "Sanity check");

    // This works
    if (success) {
        LOG_ERROR("Successful");
    } else {
        LOG_ERROR("Unsuccessful"); // Log's written
    }

    // This doesn't work
    if (success)
        LOG_ERROR("Successful (w/o)");
    else
        LOG_ERROR("Unsuccessful (w/o)"); // Log's missing
}

Depending on the value of "success" the following now happens (BOOST_ASSERT_MSG is adapted accordingly): If success == true, "Successful" and "Successful (w/o)" is written to the log. If success == false, "Unsuccessful" is written, but "Unsuccessful (w/o)" is not.

Of course I also checked the logging and wanted to end the program with exit(0) instead of writing logs, but this code didn't work either:

void dbg_test() {
    bool success = false;

    BOOST_ASSERT_MSG(!success, "Sanity check");

    // This works
    if (success) {
        LOG_ERROR("Successful");
    } else {
        exit(0);
    }

    // This doesn't work
    if (success)
        LOG_ERROR("Successful (w/o)");
    else
        exit(0);
}

For compilation and debugging I use Microsoft Visual Studio Community 2017 (Version 15.5.0). The corresponding project folder was created with CMake 3.13.4.

What could be the reason for this?

Aucun commentaire:

Enregistrer un commentaire