In the codebase I'm working, we use std::any
instead of void*
to pass classes through some generic non-template code. Specifically, we use Visual Studio 2019, its compiler and standard library.
In order to visualize the std::any
, microsoft already gives a natvis:
<Type Name="std::any">
<Intrinsic Name="has_value" Expression="_Storage._TypeData != 0"/>
<Intrinsic Name="_Rep" Expression="_Storage._TypeData & _Rep_mask"/>
<Intrinsic Name="type" Expression="(const type_info*)(_Storage._TypeData & ~_Rep_mask)"/>
<Intrinsic Name="_Is_trivial" Expression="has_value() && _Rep() == 0"/>
<Intrinsic Name="_Is_big" Expression="has_value() && _Rep() == 1"/>
<Intrinsic Name="_Is_small" Expression="has_value() && _Rep() == 2"/>
<DisplayString Condition="!has_value()">[empty]</DisplayString>
<DisplayString Condition="_Is_trivial() || _Is_small()">[not empty (Small)]</DisplayString>
<DisplayString Condition="_Is_big()">[not empty (Large)]</DisplayString>
<Expand>
<Synthetic Name="has_value">
<DisplayString>{has_value()}</DisplayString>
</Synthetic>
<Synthetic Name="type" Condition="has_value()">
<DisplayString>{type()}</DisplayString>
</Synthetic>
<Synthetic Name="[representation]" Condition="_Is_trivial()">
<DisplayString>(Small/Trivial Object)</DisplayString>
</Synthetic>
<Synthetic Name="[representation]" Condition="_Is_small()">
<DisplayString>(Small Object)</DisplayString>
</Synthetic>
<Synthetic Name="[representation]" Condition="_Is_big()">
<DisplayString>(Dynamic Allocation)</DisplayString>
</Synthetic>
</Expand>
</Type>
However, this ends up showing us (Small Object)
instead of the std::string
that we have stored into it.
I've already managed to extend this with a few extra lines to get the pointer to the data:
<Item Name="[castable_ptr]" Condition="_Is_trivial()">(void*)(&_Storage._TrivialData)</Item>
<Item Name="[castable_ptr]" Condition="_Is_small()">(void*)(&_Storage._SmallStorage._Data)</Item>
<Item Name="[castable_ptr]" Condition="_Is_big()">(void*)(_Storage._BigStorage._Ptr)</Item>
However, this shows the data as a void*
, which you have to manually cast to a pointer of the actual type std::string*
.
However, this std::any
implementation/visualization also comes with a std::type_info
. (see field: type) that knows which underlying type we have.
Is there a way to use this std::type_info
so that the (void*)
can be replaced by a cast to the actual stored type?
EDIT: An example of the information that visual studio provides for the type: {mydll.dll!class std::tuple<__int64,double,double,double> 'RTTI Type Descriptor'} {...}
When explicitly casting the address to std::type_info*, I get access to _Data
in the debugger, that contains _DecoratedName
(.?AV?$tuple@_JNNN@std@@
) and _UndecoratedName
(nullptr
).
Unfortunately, I can't seem to find out how to write a cast that leverages this information.