1

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.

1
  • You can use a cast in the watch window. As you have erased the type the debugger needs some help. Commented Dec 17, 2021 at 17:27

1 Answer 1

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.