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对象是否包含值。

#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;
}

相关推荐

  1. CPP Weekly --C++17

    2024-02-03 18:28:03       45 阅读
  2. c++17--iota

    2024-02-03 18:28:03       36 阅读
  3. <span style='color:red;'>10</span> <span style='color:red;'>C</span>++<span style='color:red;'>11</span>

    10 C++11

    2024-02-03 18:28:03      23 阅读
  4. c语言(7.17

    2024-02-03 18:28:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 18:28:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 18:28:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 18:28:03       82 阅读
  4. Python语言-面向对象

    2024-02-03 18:28:03       91 阅读

热门阅读

  1. 2024/2/2 备战蓝桥杯 4-1 排序

    2024-02-03 18:28:03       50 阅读
  2. oracle 修改表结构语句

    2024-02-03 18:28:03       44 阅读
  3. AIGC开发 -- 本地方法与AI的互动Function calling

    2024-02-03 18:28:03       55 阅读
  4. 内核升级!IvorySQL 3.1 发版

    2024-02-03 18:28:03       49 阅读
  5. MySQL中的视图与索引

    2024-02-03 18:28:03       48 阅读
  6. ES5/ES6 的继承除了写法以外还有什么区别?

    2024-02-03 18:28:03       45 阅读
  7. Unity3D开发之鼠标单双击判断

    2024-02-03 18:28:03       52 阅读
  8. 大小相等的numpy数组运算及数组与标量的运算

    2024-02-03 18:28:03       47 阅读
  9. LeetCode每日一题 | 1686. 石子游戏 VI

    2024-02-03 18:28:03       61 阅读
  10. How to switch CRAN of R language to Tsinghua mirror in ubuntu

    2024-02-03 18:28:03       58 阅读
  11. Ubuntu 关闭rsyslog,var/log/syslog文件过大解决

    2024-02-03 18:28:03       48 阅读