C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Modularized logging library - prevent parameter evaluation before calling

From: Rainer Deyke <rainerd_at_[hidden]>
Date: Wed, 28 Oct 2026 10:36:37 +0100
On 10/28/26 10:00, Alexander Christensen via Std-Proposals wrote:
> Hi,
>
> Someone reminded me of my erroneous logging function:
> LogWarn("Invalid Value {}", GetValue());
>
> The function is defined like this:
> template<typename... Ts>
> void LogWarn(std::string_view fmt, Ts&&... args)
> {
> #if defined(LOG_WARN)
> // log...
> #endif
> }

You can do this by making LogWarn a macro. You *should* do this by
making LogWarn a macro.

One guarantee in C++ is that given functions f and g in the code f(g()),
function f cannot prevent g from being called or affect the evaluation
of g() in any way. It's a tradeoff that makes code easier to understand
at the cost of limiting the expressive power of the language. And there
already is an escape hatch: turn f into a macro, and the guarantee goes
away.

You are proposing a second escape hatch, presumably because you don't
like macros. I don't think that's a good enough reason.

In some other languages, given the equivalent of f(g()), the function f
can control when and if g() is evaluated. And that's fine for those
languages. It comes at a significant cost in conceptual complexity, but
it allows these languages to simplify in other ways (such as by turning
flow control structures into regular functions). But C++ doesn't have
to, and shouldn't, imitate them.


-- 
Rainer Deyke - rainerd_at_[hidden]

Received on 2026-10-28 09:36:46