CPP Weekly --C++17

C++17

std::any

std::any库,它是一种类型安全的容器,可以存储任意类型 的数据。与void*不同,std::any可以检查是否存储了值,并且可以安全地检索和转换存储的值。我们可以使用std::any_cast函数检索该值,并将其转换为期望的类型,如果类型匹配,则强制转换返回相应的地址指针,如果不匹配,则返回nullptr。若要修改该值,需要转换为对应的引用类型 。如果我们试图从std::any对象中检索不正确的类型,std::bad_any_cast异常将被抛出。此外,如果std::any对象没有存储任何值,则std::bad_any_cast异常也会被抛出。因此,在使用std::any_cast函数时应谨慎,最好使用std::any::has_value函数检查std::any对象是否包含值。

c 复制代码
#include <iostream>
#include <any>
#include <string>
#include <vector>
#include <exception>

int main(int argc, char** argv)
{
    std::any a;
    try {
        std::cout << std::any_cast<int>(a) << std::endl;
    } catch (std::exception& e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    // check has value
    std::cout << "std::any::has_value(): " << a.has_value() << std::endl;
    // clear value
    a.reset();  // a = std::any{}; 或 a = {};

    std::cout << "std::any::has_value(): " << a.has_value() << std::endl;

    a = 1;
    std::cout << "std::any::has_value(): " << a.has_value() << std::endl;
    if (a.type() == typeid(int)) {
        std::cout << "std::any::has_value type int" << std::endl;
    }

    std::cout << std::any_cast<int>(a) << std::endl;

    a = 3.1415926;  // double
    std::cout << std::any_cast<double>(a) << std::endl;
    if (a.type() == typeid(double)) {
        std::cout << "std::any::has_value type double" << std::endl;
    }

    a = std::string("Hello");  // std::string
    std::cout << std::any_cast<std::string>(a) << std::endl;
    // modify value
    std::any_cast<std::string&>(a) = "World";
    std::cout << std::any_cast<std::string>(a) << std::endl;

    std::vector<int> v1 = {1, 2, 3, 4, 5};
    a                   = v1;  // vector
    std::vector<int> b  = std::any_cast<std::vector<int>>(a);
    for (auto i : b) {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    a = {};  // clear value
    std::cout << "---------------------" << std::endl;
    std::vector<std::any> v2;
    v2.push_back(42);
    std::string s = "hello";
    v2.push_back(s);
    for (const auto& tmp : v2) {
        if (tmp.type() == typeid(std::string)) {
            std::cout << "type string: " << std::any_cast<const std::string&>(tmp) << std::endl;
        } else if (tmp.type() == typeid(int)) {
            std::cout << "type int: " << std::any_cast<int>(tmp) << std::endl;
        }
    }

    return 0;
}
相关推荐
froginwe1111 分钟前
R 基础运算
开发语言
醉城夜风~12 分钟前
[数据结构]堆详解
开发语言·数据结构
刃神太酷啦28 分钟前
数据结构(蓝桥杯常考点)
数据结构·c++·蓝桥杯c++组
17´38 分钟前
Qt从入门到入土(八) -打包Qt程序
开发语言·c++·qt
AI+程序员在路上39 分钟前
QT显示网页控件QAxWidget、QWebEngineView及区别
开发语言·qt
南玖yy1 小时前
C语言柔性数组深度解析:动态内存管理的艺术
c语言·开发语言·柔性数组
2301_764441331 小时前
python实现的生态模拟系统
开发语言·python·pygame
无世世1 小时前
【Java从入门到起飞】面向对象编程(高级)
java·开发语言
q567315231 小时前
使用CPR库编写的爬虫程序
开发语言·爬虫·golang·音视频
星之卡比*1 小时前
前端0基础---day18Math - Date - 定时器 (javascript)
开发语言·前端·javascript