javaLover javaLover - 3 months ago 9
C++ Question

Display void* (array) with known type and length in debugger automatically

Assume that type

T
and length
size
is known,
how to display array
data
in debugger gracefully?

template<class T>class Container{
void* data; //<-- display it!
int size;
}


In watch windows (Visual studio 2015), I can display
container.data
by typing:-

static_cast<T*>(container.data),size


Question: Are they any technique (especially modify code in
Container
) to make this process to be automatic and elegant - like std::vector?

In other words, it would be nice if I can just type
container
, then the watch will show :-

container
+data (the + button, can click to expand)
--data[0] (expanded)
--data[1]
......
--data[size-1]


My best clue is to use
union
, but I am not sure.

Answer

VS2015 allows debug visualization customization via custom .natvis configuration.

Among the other features it has support for the templated classes. The Name attribute of the Type element accepts an asterisk * as a wildcard character that can be used for templated class names. To refer template parameter in the visualization entry you can use $T1 macros. Examples are located in VS2015 Microsoft Visual Studio 14.0\Common7\Packages\Debugger\Visualizers folder.

Comments