Reputation: 480
I would like std::map<string, any> *var1
to point to the same memory address/value as std::any *var2
because I know that var2
is pointing to a map<string, any>
.
The following seems to work:
std::map<string, any> *var1 = any_cast<std::map<string, any>>(var2);
Is it ok? The problem is that it does not signal bad cast even if var2
is actually not an std::map<string, any>*
but something else, but if it is it still works.
Am I doing it right?
std::map<string, any> *mapptr;
std::any realmap = std::map<string, any>({{"key", "value"}, {"key2", 5}});
std::any *ptrtomap = &realmap;
mapptr = any_cast<std::map<string, any>>(ptrtomap);
// Interesting part below
realmap = 6;
mapptr = any_cast<std::map<string, any>>(ptrtomap);
After changing the type of the variable to integer the any pointer is pointing to I can still cast that pointer to a pointer of type std::map
.
What???
Upvotes: 1
Views: 1416
Reputation: 66371
Just like dynamic_cast
, any_cast
ing a pointer returns the null pointer when it fails - it does not throw an exception.
Upvotes: 1
Reputation: 9715
The problem is that it does not signal bad cast even if var2 is actually not an std::map* but something else
As reported in the documentation of std::any (overload n.5):
If operand is not a null pointer, and the typeid of the requested T matches that of the contents of operand, a pointer to the value contained by operand, otherwise a null pointer.
So actually you can check the cast's successful just checking the pointer.
mapptr = std::any_cast<std::map<string, any>>(&realmap);
if (!mapptr) {
// invalid cast conversion
}
Upvotes: 2
Reputation: 1567
Try running this code
#include <stdio.h>
#include <map>
#include <string>
#include <any>
using namespace std;
int main()
{
map<string, any> *amap;
any realmap = std::map<string, any>({{"key", "value"}, {"key2", 5}});
any *ptrtomap = &realmap;
amap = any_cast<map<string, any>>(ptrtomap);
printf("Pointer real map: %x\n", amap);
// Interesting part below
realmap = 6;
amap = any_cast<map<string, any>>(ptrtomap);
printf("Pointer invalid map: %x\n", amap);
return 0;
}
it outputs e.g.
Pointer real map: 11cfd00
Pointer invalid map: 0
So the illegal conversion returned a null pointer. The compiler can't detect invalid casts
Upvotes: 2