gpt4 book ai didi

C++ 部分模板特化和 Natvis

转载 作者:行者123 更新时间:2023-11-30 04:43:04 24 4
gpt4 key购买 nike

我正在尝试为部分专用类型创建 Visual Studio 调试可视化工具。例如,假设我有这样的东西:

template <typename T>
struct Foo
{
T bar;
};

template <typename T>
struct Foo<T*>
{
T baz;
};

如果没有偏特化,这会很容易:

<Type Name="Foo&lt;*&gt;"> ... </Type>

有了完全特化,这也很容易:

<Type Name="Foo&lt;int&gt;"> ... </Type>

但是我如何涵盖部分特化?这甚至受支持吗?如果没有,是否有解决方法?

最佳答案

简短回答 - 不可以。您不能在 natvis 类型名称中指定类型限定符、引用等 <Type Name="Foo&lt;*&gt;"> .

但是:

您可以将模板的 typename 参数用作字符串并与类型进行比较。例如,在节点的 Condition属性:

<Type Name="Foo&lt;*&gt;">
<DisplayString Condition="strcmp(&quot;$T1&quot;,&quot;short&quot;)==0">specialization short</DisplayString>
<DisplayString Condition="strcmp(&quot;$T1&quot;,&quot;int &amp;&quot;)==0">specialization int &amp;</DisplayString>
<DisplayString>unspecified specialization</DisplayString>
</Type>

对于 Foo<short>你会看到specialization shortunspecified specialization对于其他。

例子:

template <typename T, typename U>
struct Foo
{
T bar;
};

template <typename U>
struct Foo<int &, U>
{
U baz;
};
int main()
{
int gg = 0;
Foo<short, int> a;
Foo<int, int> b;
Foo<int &, int> c;

纳维斯:

<Type Name="Foo&lt;*,*&gt;" >
<DisplayString Condition="strcmp(&quot;$T1&quot;,&quot;short&quot;)==0">specialization short</DisplayString>
<DisplayString>unspecified specialization</DisplayString>
</Type>

<Type Name="Foo&lt;int&amp;,*&gt;">
<DisplayString>partial specialization int&amp;</DisplayString>
</Type>

结果:

strcmp

或者你,如果你在你的偏特化类型中有一些独特的成员,可以使用 Priority选项。

例子:

template <typename T>
struct Foo
{
T bar;
};

template <typename U>
struct Foo<U &>
{
U baz;
};
int main()
{
int g = 0;
Foo<short> a;
Foo<int> b;
Foo<int &> c{g};

纳维斯:

<Type Name="Foo&lt;*&gt;">
<DisplayString>partial specialization {baz}</DisplayString>
</Type>

<Type Name="Foo&lt;*&gt;" Priority="Low">
<DisplayString>other specialization</DisplayString>
</Type>

结果:

Priority

关于C++ 部分模板特化和 Natvis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com