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;
}
相关推荐
嵌入式进阶行者5 分钟前
【算法】深度优先搜索实例:华为OD机考双机位A卷- 中庸行者
c++·算法·华为od·深度优先
Knight_AL14 分钟前
Java 多态详解:概念、实现机制与实践应用
java·开发语言
Omigeq18 分钟前
1.2.1 - 图搜索算法(以A*为例) - Python运动规划库教程(Python Motion Planning)
开发语言·python·机器人·图搜索算法
资深流水灯工程师20 分钟前
基于Python的Qt开发之Pyside6 串口接收数据被分割的解决方案
开发语言·python·qt
没有bug.的程序员22 分钟前
Java 并发容器深度剖析:ConcurrentHashMap 源码解析与性能优化
java·开发语言·性能优化·并发·源码解析·并发容器
云深麋鹿40 分钟前
C++入门篇
c++
量子炒饭大师1 小时前
【C++入门】零域终端的虚空指针协议——【nullptr】还在为编译器给NULL匹配为int而头疼?nullptr给予你全新的字面量!
开发语言·c++·nullptr
edisao1 小时前
一。星舰到底改变了什么?
大数据·开发语言·人工智能·科技·php
阿豪只会阿巴1 小时前
【多喝热水系列】从零开始的ROS2之旅——Day10 话题的订阅与发布1:Python
开发语言·c++·python·ubuntu·ros2
羊小猪~~1 小时前
【QT】--文件操作
前端·数据库·c++·后端·qt·qt6.3