std::any

#include <iostream>
#include <any>

void read1(std::any &data)
{
        data = std::move("no");
}

void read2(std::any &data)
{
        data = std::move(std::string("yes"));
}

int main()
{
        std::any a;

        read1(a);
        if(a.has_value())
        {
                std::cout << "hello furong" << std::endl;
                std::cout << a.type().name() << std::endl;
        }

        if(a.type() == typeid(const char*))
        {
                auto s = std::any_cast<const char*>(a);
                std::cout << s << std::endl;
        }

        a.reset();

        read2(a);
        if(a.has_value())
        {
                std::cout << "hello quange" << std::endl;
                std::cout << a.type().name() << std::endl;
        }

        if(a.type() == typeid(std::string))
        {
                auto s = std::any_cast<const std::string&>(a);
                std::cout << s << std::endl;
        }

        return 0;
}
hello furong
PKc
no
hello quange
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
yes
posted @ 2023-04-20 17:50  thomas_blog  阅读(59)  评论(0编辑  收藏  举报