简体   繁体   中英

Using a runtime string to specify object type in natvis

Say I have a struct

struct Foo
{
    void* bar;
    const char* barTypeName;
}

bar is some type erased thing and barTypeName is a proper C++ type identifier that identifies the actual type of bar .

I want to visualize this in the Visual Studio debugger, particularly in the Watch window. There's no template involved that can give me the proper type. The type itself is frequently POD and the debugger isn't able to figure out the type automatically.

Question: Is there any way in natvis to tell the debugger the type of bar so it displays properly in the Watch window?

I stumbled on <MostDerivedType> in the natvis schema , but it's not documented as far as I can tell and I can't tell if it does what I'm after or not.

I'm happy enough to use <CustomVisualizer> and implement this in C++ if it provides a way to handle this and natvis does not.

That is quite simple if you are willing to add a DisplayString for each wrapped POD. If you want a generic solution, that might not be possible.

<Type Name="Foo">
  <DisplayString Condition='strcmp(barTypeName,"char")'>{(char)bar}</DisplayString>
  <DisplayString Condition='strcmp(barTypeName,"int")'>{(int)bar}</DisplayString>
</Type>

Code for testing:

char c{};
int i{};
Foo fooc{ &c, "char" };
Foo fooi{ &i, "int" };

And this is the result in the VS 2019 (16.11.11) debugger:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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