简体   繁体   English

C++ 部分模板专业化和 Natvis

[英]C++ Partial Template Specialization and Natvis

I'm trying to create Visual Studio debug visualizers for a partially specialized type.我正在尝试为部分专业化类型创建 Visual Studio 调试可视化工具。 For example, let's say I have something like this:例如,假设我有这样的事情:

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

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

Without the partial specialization, this would be easy:如果没有部分专业化,这将很容易:

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

With full specialization, it'd also be easy:有了完全的专业化,这也很容易:

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

But how do I cover the partial specialization?但是我如何涵盖部分专业化? Is this even supported?这甚至被支持吗? If not, is there a workaround?如果没有,是否有解决方法?

Short answer - No. You can't specify type qualifiers, references, and so on in natvis type name <Type Name="Foo&lt;*&gt;"> .简短回答 - 不。您不能在 natvis 类型名称<Type Name="Foo&lt;*&gt;">中指定类型限定符、引用等。

But:但:

You can use the template's typename parameter as a string and compare with a type.您可以将模板的 typename 参数用作字符串并与类型进行比较。 For example, in the node's Condition attribute:例如,在节点的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>

For Foo<short> you will see specialization short and unspecified specialization for other.对于Foo<short> ,您将看到其他的specialization shortunspecified specialization

Example:例子:

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;

Natvis:纳特维斯:

<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>

Result:结果:

strcmp

Or you, if you have some unique member in your partially specialized type, can use Priority option.或者,如果您的部分专业类型中有一些独特的成员,则可以使用Priority选项。

Example:例子:

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};

Natvis:纳特维斯:

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

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

Result:结果:

优先

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM