3

I've written a simple (and potentially bad) implementation for a vector class, similar to std::vector.

Here is the class:

template <class T>
class Vector
{
    T* data;
    int size;
public:
    Vector(int = 0);
    ~Vector();

    Vector(const Vector<T>&);
    
    Vector<T>& operator=(Vector<T>);
    T& operator[](int);

    friend void swap(Vector<T>&, Vector<T>&);

    void Clear();
    void Insert(T, int);
    void Delete(int);
    int Size();
};

When debugging code that uses my vector, I've noticed that the pointer that I'm using internally only expands up to the first element normally.

I found this SO question, How to display a dynamically allocated array in the Visual Studio debugger?, which seems to give a simple solution to the problem, but I'm wondering if it's possible to expand the array by a non-constant amount (say, the current vector size).

Considering that std::vector does show all of its elements normally inside the debugger, could I alternatively rewrite my vector to include that functionality?

Here is a snip of the "Locals" tab with some test variables, to show what I'm referring to:

Community
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] and [edit] your question to show it. – Some programmer dude Apr 09 '20 at 08:42
  • @Someprogrammerdude I don't think that applies here. The question is not about debugging code. It is about customizing pretty printing in the debugger and I think the problem is sufficiently clearly stated. – walnut Apr 09 '20 at 08:45
  • I wonder if this could be done with a [Client Block Hook Function](https://learn.microsoft.com/en-us/visualstudio/debugger/debug-hook-function-writing?view=vs-2019) that reports info to the VC debugger - but all the examples I've seen simply write information to stdout - I suppose you might have to use COM (`EnvDTE` etc) inside your `_CrtSetDumpClient` callback - but that would balloon the complexity of your code. – Dai Apr 09 '20 at 08:59
  • FWIW I couldn't find any information regarding any custom `__declspec` attributes for specifying debugger behaviour (unlike .NET Framework's `[DebuggerDisplayAttribute]` ). That's a shame - and rather surprising. (To be fair, you _can_ use `DebuggerDisplay` in C++ code, but only C++/CLI - which isn't "real" C++...) – Dai Apr 09 '20 at 09:02

1 Answers1

2

It seems that I found how to do this using .natvis files.

This article provides more details about Natvis files: https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019

Adding a .natvis file to your project allows you to specify how the container should be displayed in Locals.

Here is a simple example for the Vector container described in the original post:

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="AC::Vector&lt;*&gt;">
    <DisplayString>{{ size={size} }}</DisplayString>
    <Expand>
      <Item Name="[size]" ExcludeView="simple">size</Item>
      <ArrayItems>
        <Size>size</Size>
        <ValuePointer>data</ValuePointer>
      </ArrayItems>
    </Expand>
  </Type>

</AutoVisualizer>

After creating the file and starting the debug session, the container now displays its contents properly:

AC::Vector<int> myVec(3);
myVec[0] = 1;
myVec[1] = 2;
myVec[2] = 3;

Locals:

enter image description here