I am writing a wrapper class template around a byte storage and I use a constexpr std::array to store the offsets of the members (some can be missing based on the template). The code runs as expected but as I went to write a natvis file for it, it seems that in the release configuration, the constexpr std::array is undefined. Below is a trimmed down version of the problem:
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 File:
<?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: Error: identifier "TestArray" is undefined.
Note that I need to be able to access the offset array in my actual use case from within natvis to fetch the offsets so that I can use them to access objects in the byte storage.
I was expecting the constexpr to exist in release configurations as well. It makes sense that it gets compiled out, but constexpr primitives still remain accessible and usable by natvis. I can store a particular entry in the array and use it like this:
static constexpr size_t Element2 = TestArray[2];
But why is the array undefined when the above works? I also tried storing a static volatile pointer to the constexpr array, using a C-style array, and using a member function that returns a pointer to the array to see if natvis can use that.
EDIT: The compiler I am using is msvc 2022 v143
EDIT: Tried the /Zo compiler option as mentioned by user IInspectable but that does not seem to have an effect on the error
EDIT: Tried replacing <Item... with <ExpandedItem... but got the same error.
TestArrayto get optimized away in a Release configuration. Can you try adding the/Zocompiler option to see if that changes things? Also, since NatVis is notoriously debugger-specific, could you add the specific debugger you are using to your question?/Zocompiler option doesn't affect code generation. It controls the amount of data dumped into the program database (PDB). The PDB is what drives NatVis evaluation, so that's why I suggested having the compiler produce more complete data. Since that did not work out, here's another idea (that probably won't succeed either): Try using<ExpandedItem ...instead of<Item ....<ExpandeItem...>as well and I get the same error:Error: identifier "TestArray" is undefined1staticdata members do not show up as a child of their enclosing class (you can easily verify this), or2the symbol doesn't survive compilation and doesn't show up in the PDB. I used to have a command-line tool that dumps all types from a PDB, but can't seem to find it. If it turns up I'll let you know.