【问题标题】:Result of unpacking variadic template - Visual studio 2019 CE解压可变参数模板的结果 - Visual Studio 2019 CE
【发布时间】:2020-08-30 09:42:49
【问题描述】:

出于调试目的,我想查看在 c++ 中解压缩可变参数模板的结果。在VS中无论如何都可能吗?

//variadic template
template<class ... Ts>
void foo(Ts...args)
{
//...some code 
auto x = bar(args..);
}
    
//client code:
foo(std::string("123"),int(4),5.6f);

我认为代码生成的结果会被解压成这样的:

void foo<std::string, int, float>(std::string args1, int args2, float args3)
{
//...some code 
auto x = bar(args1,args2,args3);
}

但如果它更难一点,谁知道它会解压成什么? :) 在尝试找到它之前,我认为如果在 VS 中将“属性->C/C++->预处理器->预处理到文件”切换为“是”,它将在预处理期间生成,我们可以看到这一点。但不是,在这个阶段它只适用于标题,#incldude's #define's ... 问题是如何查看解包参数包代码生成的结果?这段代码是否以可读的形式存在?或者结果翻译成二进制和*.exe文件?

【问题讨论】:

  • 不确定VS能不能做到这一点,但你们cppinsights可以:cppinsights.io/s/00cd7215
  • 是的!这正是我所需要的。我无法想象 VS 中缺少这个“必须具备的功能”。

标签: c++ visual-studio visual-studio-2019 variadic-functions visual-studio-debugging


【解决方案1】:

你可以做的是一种静态印刷。

静态打印将停止编译并显示参数的错误

template <typename...> 
struct TParamViewer;

#define DEBUG_PRINT

#ifdef DEBUG_PRINT
#define VIEW_TPL_PARAMS(index, params) TParamViewer<params> td##index;
#else
#define VIEW_TPL_PARAMS(index, params)
#endif

//variadic template
template<class ... Ts>
void foo(Ts...args)
{
    VIEW_TPL_PARAMS(0, Ts...);
}

静态打印会显示警告并显示参数

在这里,我们向查看器类添加一个定义,该类将显示一个警告,显示模板参数。

template <typename...> 
struct TParamViewer {
    bool i = 10;
};

所有这些解决方案都是把戏。在 gcc 中,有一个 proposed patch 用于正确的调试打印。

问候,

【讨论】:

  • 这揭示了模板函数中写入的参数以打开(实际上它已经知道传递了哪些类型)。但它不显示打开模板的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2013-01-26
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
相关资源
最近更新 更多