0

std:::any e = 1.0f;

And this is what I get in VS watch:

VS Watch

Is there a way to get the value and the type?

If I use std::any_cast<float>(e) it works, but what If I don't know the type.

If I use any other <> invalid type in std::any_cast I guess that the debugger crashes. It detaches from my app and then I get an invalid type message in the watch window.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • 1
    I guess you don't. Yet another reason to avoid `std::any`. BTW `(float)1.0f` is completely redundant. – Passer By Oct 15 '22 at 05:30
  • 1
    Don't know if the debugger allows it, but `e.type().name()` should give you some string describing the type in some form. – user17732522 Oct 15 '22 at 05:50

1 Answers1

0

Regarding the type, you already see this in the type field (notice the float before the RTTI Type Descriptor. Unfortunately, getting the actual value in the debugger is not really possible at the moment. See this feature request on the developer community.

Note that there is a somewhat hacky workaround posted here which edits the default natvis file provided with Visual Studio and requires you to add additional C++ code to your project. Moreover, in that C++ code you need to manually enumerate all the possible values that std::any might take (see the AnyCasterService constructor).

Sedenion
  • 5,421
  • 2
  • 14
  • 42