make .natvis show SmartPointer<T> as static_cast<T*>(void*)

1.1k views Asked by At

E2<T> is a smart pointer.

To enable a tool-tip of E2<T>->aField when mouse hovers above it, I can create .natvis like :-

.cpp

class Blank{
    public: int sss=5;
};
template<class T> class E2 {
    public: T* operator->(){
        return static_cast<T*>(atDerive);
    }
    T* atDerive;          //#
};
int main(){
    Blank k2; k2.sss=32;
    E2<Blank> e2;
    e2.atDerive=&k2;
    e2->sss=4;
}

.natvis

  <Type Name="E2&lt;*&gt;">
    <SmartPointer Usage="Minimal">atDerive</SmartPointer>
  </Type>

Result

enter image description here

Problem

The above approach doesn't work if the content void* is in the base class E1.

.cpp

class E1{
    public: void* atBase=nullptr;    //#
};
template<class T> class E2 : public E1{
    public: T* operator->(){
        return static_cast<T*>(atBase);
    }
};
int main(){
    Blank k; k.sss=31;
    E2<Blank> e2;
    e2.atBase=&k;
    e2->sss=4;
}

.natvis (not work - no tool-tip appear)

  <Type Name="E2&lt;*&gt;">
    <SmartPointer Usage="Minimal">atBase</SmartPointer>
  </Type>

Question

How to modify .natvis to enable tool-tip to show void* that is in the base class (E1)?

1

There are 1 answers

2
mr NAE On BEST ANSWER

Add a type cast from void* to template type T (you can get it as a $T1 in your .natvis file)

<Type Name="E2&lt;*&gt;">
  <SmartPointer Usage="Minimal">($T1*)atBase</SmartPointer>
  <DisplayString>{($T1*)atBase}</DisplayString>
</Type>