函数本地 structs/classes 和 natvis 文件
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
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<*,*>">
<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 确实显示了对本地类型的正确解释,具有讽刺意味的是,它显示了之前无法解释的本地类型的名称:
假设我必须遵循结构:
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
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<*,*>">
<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 确实显示了对本地类型的正确解释,具有讽刺意味的是,它显示了之前无法解释的本地类型的名称: