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.

5
  • Why not just declare data as T* data;? Commented Aug 27, 2016 at 7:34
  • I want to avoid default constructor, so I use placement new. stackoverflow.com/a/4756306/3577745 Commented Aug 27, 2016 at 7: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). Commented Aug 27, 2016 at 7: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++) Commented Aug 27, 2016 at 7: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. Commented Aug 27, 2016 at 7:47

1 Answer 1

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.

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.