Using the pointer-form of any_cast
is much cleaner, as it uses the nullability of pointers:
for (const auto & elem : m)
if (T1* p = any_cast<T1>(&elem))
{
do stuff with *p;
}
else if (T2* p = any_cast<T2>(&elem))
{
do stuff with *p;
}
else if (...)
{
...
}
This also has the advantage of doing the cast once per case.