std::source_location
用于捕获函数调用的上下文信息。当我尝试将其与可变模板函数一起使用时,遇到了一个问题:我找不到放置source_location
参数的位置。以下代码不起作用,因为可变参数必须在最后:
// doesn't work
template <typename... Args>
void debug(Args&&... args,
const std::source_location& loc = std::source_location::current());
以下代码也无法正常工作,因为调用者会受到插入在参数中间的影响而出现问题:
// 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
我在评论中得知,std::source_location
与可变参数模板无缝协作,但我不知道如何操作。请问如何在可变参数模板函数中使用 std::source_location
?
debug
定义为一个宏,该宏将在正确的参数位置(首个)使用std::source_location::current()
调用真正的“debug”函数? - Some programmer dudestd::source_location
的初衷,我的意见是这样的。 - L. F.