2017-09-26 13 views
2

比方說,我有以下結構:函數的局部結構/類和natvis文件

template<class Type, int32 SIZE> 
struct TSH2SizedArray 
{ 
    inline void Add(const Type & Value); 


    inline Type & operator[](int32 Index); 
    inline const Type & operator[](int32 Index)const; 

private: 
    uint8 Data[SIZE * sizeof(Type)]; 
    int32 ElemCount = 0; 
}; 


template<class Type, int32 SIZE> 
inline void TSH2SizedArray<Type, SIZE>::Add(const Type & Value) 
{ 
    assert(0 <= ElemCount && ElemCount < SIZE); 
    *((Type*)(Data + ElemCount++ * sizeof(Type))) = Value; 
} 

template<class Type, int32 SIZE> 
inline Type & TSH2SizedArray<Type, SIZE>::operator[](int32 Index) 
{ 
    assert(0 <= Index && Index < ElemCount); 
    return *((Type*)(Data + Index * sizeof(Type))); 
} 

template<class Type, int32 SIZE> 
inline const Type & TSH2SizedArray<Type, SIZE>::operator[](int32 Index)const 
{ 
    assert(0 <= Index && Index < ElemCount); 
    return *((Type*)(Data + Index * sizeof(Type))); 
} 

而在我natvis文件中的以下內容:

<Type Name="TSH2SizedArray&lt;*,*&gt;"> 
    <DisplayString>TotalItemCount={ElemCount} (via natvis debug)</DisplayString> 
    <Expand> 
     <Item Name="TotalItemCount">ElemCount</Item> 
     <ArrayItems> 
     <Size>ElemCount</Size> 
     <ValuePointer>($T1*)Data</ValuePointer> 
     </ArrayItems> 
    </Expand> 
    </Type> 

今天,我意識到,調試援助提供該natvis文件不會在這種情況下工作:

void MyFunc() 
{ 
    struct CMyLocalStruct 
    { 
     int ValueA; 
     int ValueB; 
    }; 
    TSH2SizedArray<CMyLocalStruct, 256> Array; 
    Array.Add(CMyLocalStruct(1,2)); 
} 

但在一個工作:

// File scope 
struct CMyLocalStruct 
{ 
    int ValueA; 
    int ValueB; 
}; 
void MyFunc() 
{ 

    TSH2SizedArray<CMyLocalStruct, 256> Array; 
    Array.Add(CMyLocalStruct(1,2)); 
} 

如果有人有解決方案,我會超級感激,因爲這是一種限制。但它看起來像一個錯誤,但我。

+1

你說它「不起作用」,但不要說爲什麼。 –

回答

2

本地結構是編譯器標記不同的類型。所以MSVC給它像一個名字:

`MyFunc'::`2'::CMyLocalStruct 

Natvis着眼於線

($T1*))Data 

,並與模板參數,這是當地結構在這種情況下,並得到替換$T1

(`MyFunc'::`2'::CMyLocalStruct*)Data 

最後,報告說:

Error: identifier "`MyFunc'" is undefined 

對我來說,看起來像一個錯誤,因爲它應該繼續閱讀其餘的類型,但我不確定。


,我已經找到一種解決方法是聲明在結構模板參數的別名有using聲明:

template<class Type, int32 SIZE> 
struct TSH2SizedArray 
{ 
    inline void Add(const Type & Value); 


    inline Type & operator[](int32 Index); 
    inline const Type & operator[](int32 Index)const; 

    using myType = Type; // natvis will interpret this correctly 

private: 
    uint8 Data[SIZE * sizeof(Type)]; 
    int32 ElemCount = 0; 
}; 

然後使用別名:

<Type Name="TSH2SizedArray&lt;*,*&gt;"> 
    <DisplayString>TotalItemCount={ElemCount} (via natvis debug)</DisplayString> 
    <Expand> 
     <Item Name="TotalItemCount">ElemCount</Item> 
     <ArrayItems> 
     <Size>ElemCount</Size> 
     <ValuePointer>(myType*)Data</ValuePointer> 
     </ArrayItems> 
    </Expand> 
    </Type> 

最後natvis確實顯示了本地類型的正確解釋,具有諷刺意味的是,它顯示了之前無法解釋的本地類型的名稱: natvis showing values

+0

感謝您的質量答覆,它解釋了爲什麼它不起作用。 – OeilDeLance

+0

但是使用 Data 不是一個解決方案,因爲整個要點是將uint8指針轉換爲CMyLocalStruct指針。爲了更容易地調試數組,你知道嗎? – OeilDeLance

+0

因此它在手錶中顯示爲CMyLocalStruct – OeilDeLance