Wrote your class string - String . But I cannot see the value of a variable of type String in Visual Studio or Qt Creator (I am translating to std::string ).

How to make the IDE show the value of such variables?

  • 3
    There is a method, you need to tell Visual Studio where to get the necessary information. I'll write how to get to the computer. - VladD

1 answer 1

See it.

For newer versions of Visual Studio (starting from 2012), it is best to use the natvis format. In this case, you can, for example, turn on the visualization of your types directly into the project, and Visual Studio will even catch changes to the display on the fly, while debugging.

Let's take a simple example. Create a simple custom class that represents a string.

 #include "stdafx.h" #include <cstring> namespace utils { class MyString { size_t length; char* payload; public: MyString() : length(0), payload(nullptr) { } MyString(char* data) : length(strlen(data)), payload(new char[length + 1]) { strcpy_s(payload, length + 1, data); } ~MyString() { delete[] payload; } }; } 

And the simplest test:

 int main() { utils::MyString s1; utils::MyString s2("I am string"); } 

Now add our custom visualization. To do this, go to the project window

add file...

and add a new natvis file. Let's call it MyString.natvis.

... added

Get the following file:

 <?xml version="1.0" encoding="utf-8"?> <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> </AutoVisualizer> 

Add our visualization to it. Put the Type tag with the name of our type (including the namespace). The subtag DisplayString specifies how the type will be displayed on a single line. You can add a condition ( DisplayString Condition= ), depending on which value will be displayed differently. The Expand subtag controls how the data will be displayed in an “open” way. (Since this is XML, do not forget that < and > for template types will have to be entered as &lt; and &gt; like Name="std::vector&lt;*&gt;" . The asterisk is used to indicate an arbitrary template argument.)

 <?xml version="1.0" encoding="utf-8"?> <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> <Type Name="utils::MyString"> <DisplayString Condition="payload == 0">[empty string]</DisplayString> <DisplayString>{payload,s}, len={length,d}</DisplayString> <Expand> <Item Name="Length">length,d</Item> <Item Name="Content">payload, sb</Item> </Expand> </Type> </AutoVisualizer> 

We get this picture in the Watch window:

here is the result

Additional reading on the topic:

  • Details on the natvis syntax and its features are located here .
  • An overview of the new features of natvis in Visual Studio 2015 is here .