[英]Function local structs/classes and natvis files
假設我必須遵循結構:
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<*,*>">
<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));
}
如果有人有解決方案,我會非常感激,因為這是一種限制。 但它看起來像是一個錯誤。
本地結構是編譯器以不同方式標記的類型。 所以MSVC給它起了一個名字:
`MyFunc'::`2'::CMyLocalStruct
納維斯看着這條線
($T1*))Data
並用模板參數替換$T1
,在這種情況下是本地結構,並得到:
(`MyFunc'::`2'::CMyLocalStruct*)Data
最后抱怨說:
Error: identifier "`MyFunc'" is undefined
對我來說,這看起來像一個bug,因為它應該繼續閱讀其余的類型,但我不確定。
我發現的一種解決方法是using
語句為struct中的template參數聲明一個別名:
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<*,*>">
<DisplayString>TotalItemCount={ElemCount} (via natvis debug)</DisplayString>
<Expand>
<Item Name="TotalItemCount">ElemCount</Item>
<ArrayItems>
<Size>ElemCount</Size>
<ValuePointer>(myType*)Data</ValuePointer>
</ArrayItems>
</Expand>
</Type>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.