Can I make ::std::any hold an unsigned integer? Something like:
::std::any a = 4;
unsigned int x = ::std::any_cast<unsigned int>(a);
results in an ::std::bad_any_cast exception because a actually holds a signed integer.
Yes, just put an unsigned integer in it:
::std::any a = 4u;
// or
::std::any a = static_cast<unsigned>(4);
https://godbolt.org/z/vzrYnKKe9
I have to caution you. std::any is not a magical tool to convert C++ into a dynamically typed language like python or javascript. Its use cases are very narrow and specific. It's not really meant to be used in user code as a "catch all" type. It's meant as a type safe alternative to void * in low level library code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With