re: Help fix my bloated C++ macros! VIEW POST


If you need the LINE (& FILE) you will have to use some top-level macro (or mention those markers explicitly on each call. Sadly, no escape from that.

In most compilers you can force inlining, and in modern C++ you can use if constrexpr to make sure that dead code is eliminated. Hence you can put the if in a function without loss.

The varg-args is a problem (and error prone, and ugly). Maybe you should think differently, like constructing a different objects for each combination of info you want to pass.


The condition is not a const-expr but a runtime value.

I'm not so concerned about the VARARGS, and I basically can get rid of it now with {}'d initializer lists. The function takes a std::vector of items. It's just a bit cleaner in the macro without the braces, but mainly that was done since the code was first written before I had the option.

Variants of the functions per object type is no an option, it would lead to too many variants. These items are really extra information passed to the logging, varying on each call site.


Within the macro body, you use

  • var-args
  • return statement

With these premises, I don't see how you could win anything unless you can really redo your exception handling approach. You are at a local optimum.

Personally I would not use var-args and a return statement within the macro body, but I have no alternative for FILE/LINE. That alone forces the use of a macro.

Did you consider using exceptions instead of codes? But you would still need a macro to supply the FILE/LINE.

I might indeed be near a local optimum. THe requirement ofr FILE/LINE, and lazy evaluation are forcing my hand.

I'm converting some of my code to use return values here instead of exceptions, since the exceptions are kind of wrong. I'm basing this on th enumber of places that need to handle the exceptions. The code has to handle these returns for other reasons anyway (failed is just one status of many).

I guess I live with it for now. :(

code of conduct - report abuse