C++ Logo

std-proposals

Advanced search

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

From: Rhidian De Wit <rhidiandewit_at_[hidden]>
Date: Wed, 28 Oct 2026 10:09:01 +0100
Hi,

You can already achieve this currently via macros:
Just an example from one of my personal projects:
#define LOG_IMPL(logLevel_, logger, format, ...)
               \
  if ((logger).logLevel <= cxxbus::LogLevel::logLevel_)
              \
  {
              \
    (logger).LogImplArgs(cxxbus::LogLevel::logLevel_, format __VA_OPT__(, )
__VA_ARGS__); \
  }

where LogImplArgs is a variadic templated function. If the if-statement
doesn't pass, then the variadic arguments of the macro are never expanded,
hence your function would never be evaluated.

I do agree however that macros aren't always the best solution, but I don't
see how this could be a language feature. Are you proposing with your
ConditionalDiscard that all parameters are dropped when IsShipping is true?

Best,

Rhidian

Op wo 28 okt 2026 om 10:00 schreef Alexander Christensen via Std-Proposals <
std-proposals_at_[hidden]>:

> 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!
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>


-- 
Rhidian De Wit
Software Engineer - TMC

Received on 2026-10-28 09:09:18