std::any的作用,可以用作判断类型

any主要作用:

1.可以用作判断当前值的是什么类型

2.any比void更加安全,而且更容易使用

struct Test
{
    int a;
    int b;
};
int main()
{
    Test a;
    a.a = 50;
    a.b = 40;
    std::any c = a;
    
    if (c.type() == typeid(Test))
    {
        auto [f,s]= any_cast<Test>(a);
        cout << f<<"and" << s << endl;
    }  
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38409301/article/details/121133499