0

I'm trying to use a macro to queue up a single log line locally in an ostringstream and then dump the entire contents of that ostringstream when the line is over. I want to still use stream insert syntax, however. So I want to turn a log line like this:

std::cerr << "Some error in my function.  Code is " << errCode << " exiting" << std::endl;

...into this

SERR("Some error in my function.  Code is " << errCode << " exiting);

I've got something simple that works well. That is, until I put it in an if-else statement. Obviously my macro is bad but I am at a loss as to what to do.

Here is a small sample program, hacked up to illustrate the problem.:

#include <iostream>
#include <sstream>
#define SERR(x)     { std::ostringstream _s; ; _s << x << std::endl; std::cerr << _s.str(); }

int main()
{
    std::cout << "Hello World!\n"; 
    bool val = false;
    if (val)
        SERR("No error");
    else
        SERR("Error"); 
}

The error message I get from the compiler in this sample is:

1>c:\users\joe\source\repos\consoleapplication5\consoleapplication5\consoleapplication5.cpp(15): error C2181: illegal else without matching if

Any ideas what I've got wrong here?

(Note I'm not in a place where I can use a 3rd Party logging solution now so it's got to be something simple like this. I could just leave all this as normal std::cerr/std::cout/std::clog messages, if I had to but I'd prefer something simple to minimize the chances of interleaving of log messages from my multithreaded app.)

1

1 Answer 1

6

Just try to expand it and see what you get:

#include <iostream>
#include <sstream>

int main()
{
    std::cout << "Hello World!\n"; 
    bool val = false;
    if (val)
        { std::ostringstream _s; ; _s << "No error" << std::endl; std::cerr << _s.str(); };
    else
        { std::ostringstream _s; ; _s << "Error" << std::endl; std::cerr << _s.str(); };
}

Notice how { } block is terminated with ;?

If you need a macro, you should always write it using do { } while (0) like this:

#define SERR(x)                                     \
    do {                                            \
        std::ostringstream _s;                      \
        _s << (x) << std::endl;                     \
        std::cerr << _s.str();                      \
    } while (0)

Not only this solves your issue, but also make it mandatory to add ; after the macro. Otherwise people could use it two ways:

SERR("foo")  // without ;
...
SERR("bar"); // with ;
2
  • 2
    So that's why I see so many macros like that (with the do-while syntax). I had never made the connection. Thank you. Been programming C++ forever but I've always been terrible in this area
    – Joe
    Aug 19, 2018 at 20:25
  • 1
    @Joe Also it is recommended wrapping arguments in parenthesis like this (x).
    – user1143634
    Aug 21, 2018 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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