如何在可变参数模板函数中使用source_location?

如果 :

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与可变参数模板函数一起使用?

皮特·斯科特尼克(Piotr Skotnicki):

通过添加推导指南,可以使第一种形式起作用:

template <typename... Ts>
struct debug
{    
    debug(Ts&&... ts, const std::source_location& loc = std::source_location::current());
};

template <typename... Ts>
debug(Ts&&...) -> debug<Ts...>;

测试:

int main()
{
    debug(5, 'A', 3.14f, "foo");
}

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用{fmt}和source_location创建基于可变参数模板的日志记录功能

如何在SWIG中包装可变参数模板类的可变参数模板成员函数?

如何正确使用可变参数模板函数

在可变参数模板中使用对

如何重载可变参数模板函数?

如何传递可变参数模板函数?

C ++:如何在可变参数模板参数上调用带有类型参数的函数?

如何使用可变参数模板参数进行模板函数调用?

如何在非变量模板类中形成可变参数模板函数?

如何在C ++中使用可变参数模板,同时保持实现类的私有性?

如何在 c++11 中使用可变参数模板生成左关联表达式(又名左折叠)?

在C ++中使用可变参数模板迭代参数包

如何访问可变参数模板函数中的参数

使用可变参数模板构建函数参数

在VS2013中使用可变参数模板时“对重载函数的歧义调用”

在可变参数模板中使用sizeof运算符可跳过函数以结束递归

是否可以在可变参数函数模板中使用“ enable_if”和“ is_same”?

使用可变参数类模板的模板参数调用可变参数函数模板?

使用成员函数调用可变参数模板函数

如何在C ++中存储可变参数模板参数?

如何在C ++中调用可变参数模板构造函数?

在可变参数模板中使用std :: placeholders

如何编写可变参数模板的递归函数?

clang不使用可变参数推断可变参数模板函数中的模板参数

在C ++中使用带有参数类型检查的可变参数模板在编译时获取函数的参数数量

如何使用可变参数模板编写通用函数包装器

如何使用可变参数模板使用std :: function

递归可变参数函数模板

编写可变参数模板构造函数