Lazy Parameter Evaluation

Viewed 1998

I'm adding a bit amount of tracing and debugging code into a class that I'm refactoring.

I have a Trace object which has some filtering properties and methods bool CanTrace(Level, , TracePropertyList = no_additional_properties) and bool Trace(Level, string, TracePropertyList = no_additional_properties).

There are already many places in the code where this trace object is used, and the string argument to the Trace method is usually some expression that I would like to avoid evaluating if I'm not going to end up outputting tracing info.

Repeating the chunk of code

if(trace.CanTrace(LEVEL_INFO, some_props))
  trace.Trace(LEVEL_INFO, consume_time().to_str(), some_props);

is ugly, and I'd like something shorter.

I was thinking about the macros

#define TRACE_WITH_PROPS(LEVEL,STRING,PROPS) //...

and

#define TRACE(LEVEL,STRING) //...

Is there a better way to do this? Possibly with templates or C++11? I don't like hiding things from the compiler with defines, and I'm doing my best to remove some macros elsewhere in this codebase.

6 Answers
Related