The C++20 feature std::source_location
is used to capture information about the context in which a function is called.
When I try to use it with a variadic template function, I encountered a problem: I can't see a place to put the source_location
parameter.
The following doesn't work because variadic parameters have to be at the end:
// doesn't work
template <typename... Args>
void debug(Args&&... args,
const std::source_location& loc = std::source_location::current());
The following doesn't work either because the caller will be screwed up by the parameter inserted in between:
// doesn't work either, because ...
template <typename... Args>
void debug(const std::source_location& loc = std::source_location::current(),
Args&&... args);
// the caller will get confused
debug(42); // error: cannot convert 42 to std::source_location
I was informed in a comment that std::source_location
works seamlessly with variadic templates, but I struggle to figure out how. How can I use std::source_location
with variadic template functions?
debug
a macro that will call the real "debug" function with thestd::source_location::current()
call at the correct argument position (first)? – Tiercelstd::source_location
in some way IMO :( – Corissaauto
is allowed in the parameter, but then we can provide42
or"foo"
as the source location. – Corissaconsteval
, actually! But it does not seem specified what happens if it is called as a template parameter. – Kraftcurrent()
– Kraftsource_location
is an actual C++ type and therefore behaves like regular C++ objects that store values. You can pass them around, store their values, and so forth, unlike macros like__LINE__
. – Vicentasource_location
still exist whether the object is created by a macro or by regular C++ logic. That's my point; it in no way invalidatessource_location
if you have aDEBUG
macro that calls your variadic function with a hand-invokedsource_location::current
. – Vicenta