C++20 特性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
可变参数模板函数?