any_cast std::any pointer to specific pointer type

≯℡__Kan透↙ 提交于 2019-12-11 06:06:16

问题


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???


回答1:


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




回答2:


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
}



回答3:


Just like dynamic_cast, any_casting a pointer returns the null pointer when it fails - it does not throw an exception.



来源:https://stackoverflow.com/questions/57429709/any-cast-stdany-pointer-to-specific-pointer-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!