C++ Logo

std-proposals

Advanced search

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

From: Alexander Christensen <alex_c007_at_[hidden]>
Date: Wed, 28 Oct 2026 09:00:49 +0000
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
}

Problem is that the function call to GetValue() is always evaluated, even if the result is not used.
It is undesireable for me to check the macro LOG_WARN in user-code every time I have to log,
and because this is a modularized approach I would like to avoid needing to include a logging header.

In my ideal world, I could define my logging function like this:
[[ConditionalDiscard = IsShipping()]]
template<typename... Ts>
void LogWarn(std::string_view fmt, Ts&&... args) { /* ... */ }

I assume the function IsShipping() to be constexpr.
And the effect would be a function call completely stripped

Only reason I didn't post on StackOverflow is because I feel we are getting inside proposal territory.

Looking forward to your inputs!

Received on 2026-10-28 09:00:54