[英]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.