如何在Natvis中访问static constexpr数组?(Release模式下)
Release模式下MSVC Natvis无法访问constexpr std::array
我正在实现一个基于字节存储的类模板,使用constexpr std::array存储成员的偏移量(部分成员是否存在由模板参数决定)。代码运行正常,但在Release配置下编写Natvis可视化规则时,发现该constexpr std::array被提示未定义。单个constexpr元素(比如单独定义static constexpr size_t Element2 = TestArray[2];)却能被Natvis正常访问。
复现代码
struct STest { static constexpr std::array<int, 4> TestArray { 1, 2, 3, 4 }; int a[4]; }; __declspec(noinline) STest* Test() { STest* sTest = new STest(); sTest->a[STest::TestArray[1]] = 5; return sTest; } int main() { std::cout << "Hello World!\n"; auto& b = *Test(); std::printf("%d\t%d\t%d\t%d\n", b.a[STest::TestArray[0]], b.a[STest::TestArray[1]], b.a[STest::TestArray[2]], b.a[STest::TestArray[2]]); }
Natvis配置文件
<?xml version="1.0" encoding="utf-8"?> <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> <Type Name="STest"> <Expand> <Item Name="TestArray">TestArray</Item> </Expand> </Type> </AutoVisualizer>
错误提示
Natvis错误提示:Error: identifier "TestArray" is undefined.
已尝试的无效方案
- 添加
/Zo编译选项 - 将Natvis中的
<Item>替换为<ExpandedItem> - 使用static volatile指针指向constexpr数组
- 改用C风格数组
- 通过成员函数返回数组指针
补充信息
- 编译器:MSVC 2022 v143
- 需求:必须在Natvis中访问该偏移数组,以此获取偏移量来操作字节存储中的对象
内容的提问来源于stack exchange,提问作者Rex240
相关产品推荐
相关产品推荐

