1

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.

javaLover
  • 6,347
  • 2
  • 22
  • 67
  • Why not just declare `data` as `T* data;`? – Drew McGowen Aug 27 '16 at 07:34
  • I want to avoid default constructor, so I use placement new. http://stackoverflow.com/a/4756306/3577745 – javaLover Aug 27 '16 at 07:35
  • @javaLover default constructor of *what* ? `data` is a pointer, regardless of whether it is `T*` or `void*`. What does default construction have to do with just declaring `T* data` ? (besides the obvious, default construction *of a pointer*). – WhozCraig Aug 27 '16 at 07:40
  • @WhozCraig If I do that, then I will have to call ... T* data=new T[size] --> default constructor called ...... Do I misunderstand it? ( I am new for C++) – javaLover Aug 27 '16 at 07:42
  • Your use case is a bit odd, but no, it doesn't *seem* like you misunderstood. Yes, `new[T]` will indeed invoke default construction. You can avoid that with cast hijinx, but this obviously isn't the place where that is being solved, and I ultimately hope you're keeping track of what has, and has not been constructed. At least I understand what you're trying to avoid. – WhozCraig Aug 27 '16 at 07:47

1 Answers1

2

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.

Nikita
  • 6,270
  • 2
  • 24
  • 37