Boost库学习笔记(四)any类型

简介: Boost库学习笔记(四)any类型

Boost库学习笔记(四)any类型

一、简介

boost的本模块提供了一个泛型的类型,这个泛型类型不同于传统意义上基于模板的编程,他可以被视为一个真正可变自由的变量,而不是被固定类型静态指定,一般有以下三种用法:

可以在不同的数据类型之间灵活转换,例如将int的5解释为string的"5",反之亦然,boost::lexical_cast支持这样的转换需求。

可以容纳不同类型的值,但是不对其在不同的类型之间进行转换,比如5保持严格的int类型,不能够隐式的转换为“5”或者5.0,对其值的解释不关心,对类型的认识有效地使它们成为安全的、通用的单一值容器,不会因模棱两可的转换而产生意外。

可以随意的引用任何能够引用的东西,不关系其类型,将所有的对底层的解释交给开发人员,利用void*,可以对未定义的行为提供了充足的空间。

二、引用

<boost/any.hpp>

namespace boost {
  class any;
  void swap(any &, any &);
  template<typename T> T any_cast(any &);
  template<typename T> T any_cast(any &&);
  template<typename T> T any_cast(const any &);
  template<typename ValueType> const ValueType * any_cast(const any *);
  template<typename ValueType> ValueType * any_cast(any *);
}

<boost/any/bad_any_cast.hpp>

namespace boost {
  class bad_any_cast;
}

<boost/any/basic_any.hpp>

namespace boost {
  namespace anys {
    template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment> 
      class basic_any;
    void swap(basic_any &, basic_any &);
    template<typename T> T any_cast(basic_any &);
    template<typename T> T any_cast(basic_any &&);
    template<typename T> T any_cast(const basic_any &);
    template<typename ValueType> const ValueType * any_cast(const basic_any *);
    template<typename ValueType> ValueType * any_cast(basic_any *);
  }
}

三、实例

#include <list>
#include <boost/any.hpp>
#include <algorithm>
#include <iostream>
using boost::any_cast;
typedef std::list<boost::any> many;
void append_int(many& values, int value) {
  boost::any to_append = value;
  values.push_back(to_append);
}
void append_string(many& values, const std::string& value) {
  values.push_back(value);
}
void append_char_ptr(many& values, const char* value) {
  values.push_back(value);
}
void append_any(many& values, const boost::any& value) {
  values.push_back(value);
}
void append_noting(many& values) {
  values.push_back(boost::any());
}
bool is_empty(const boost::any& operand)
{
    return operand.empty();
}
bool is_int(const boost::any& operand)
{
    return operand.type() == typeid(int);
}
bool is_char_ptr(const boost::any& operand)
{
    try
    {
        any_cast<const char*>(operand);
        return true;
    }
    catch (const boost::bad_any_cast&)
    {
        return false;
    }
}
bool is_string(const boost::any& operand)
{
    return any_cast<std::string>(&operand);
}
void count_all(many& values, std::ostream& out)
{
    out << "#empty == "
        << std::count_if(values.begin(), values.end(), is_empty) << std::endl;
    out << "#int == "
        << std::count_if(values.begin(), values.end(), is_int) << std::endl;
    out << "#const char * == "
        << std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl;
    out << "#string == "
        << std::count_if(values.begin(), values.end(), is_string) << std::endl;
}
struct property
{
    property() = default;
    property(const std::string&, const boost::any&);
    std::string name;
    boost::any value;
};
property::property(const std::string& name, const boost::any& value)
{
    this->name = name;
    this->value = value;
}
typedef std::list<property> properties;
class consumer
{
public:
    virtual void notify(const boost::any&) = 0;
    std::string name;
};
class school_consumer :public consumer {
public:
    school_consumer() = default;
    void notify(const boost::any& object) {
        std::cout << "notify:   "<< std::endl;
    }
};
int main(int argc, char** argv) {
    std::list<boost::any> myList;
    append_int(myList, 1);
    append_string(myList, "test");
    append_any(myList, 4);
    char cs = 's';
    const char* ptr = &cs;
    append_char_ptr(myList, ptr);
    append_noting(myList);
    //对所有类型计数
    count_all(myList,std::cout);
    //利用any的特性做配置的存储
    properties pros;
    property pro1("url", "https://xxx.com");
    property pro2("ConnnectionCount", 8);
    pros.push_back(pro1);
    pros.push_back(pro2);
    school_consumer scConsumer;
    scConsumer.notify(5);
    return 0;
}

运行结果:

2018122814580746.png

相关文章
|
机器学习/深度学习 缓存 网络安全
服务器中的conda环境
服务器中的conda环境
1830 1
|
计算机视觉 C++
C++-实现matlab的fftshift(OpenCV)
C++-实现matlab的fftshift(OpenCV)
631 0
|
安全 C++
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
2016 0
|
人工智能 监控 API
狂揽22.6k星!这个开源工具让你一键调用100+大模型,开发效率直接起飞!
LiteLLM是由BerriAI团队开发的开源项目,通过标准化OpenAI格式API接口,支持调用100+主流大语言模型(如OpenAI、Azure、Anthropic等)。其核心功能包括统一调用方式、企业级智能路由、异步流式响应及环境变量管理。项目适用于企业AI中台搭建、多模型对比测试、教育科研实验等场景。技术架构涵盖接口层、路由层、管理层与监控层,提供高效稳定的服务。相比LangChain、LlamaIndex等项目,LiteLLM在多平台混合开发方面优势显著。项目地址:https://github.com/BerriAI/litellm。
2683 2
|
11月前
|
机器学习/深度学习 自然语言处理 并行计算
基于DJL的机器学习
本文介绍了基于Java的深度学习框架DJL,涵盖机器学习与深度学习的核心概念、神经网络结构及生命周期,并通过MNIST数据集展示了从模型构建、训练到推理的完整流程。内容深入浅出,适合初学者入门。
704 5
基于DJL的机器学习
|
消息中间件 存储 负载均衡
AI 推理场景的痛点和解决方案
一个典型的推理场景面临的问题可以概括为限流、负载均衡、异步化、数据管理、索引增强 5 个场景。通过云数据库 Tair 丰富的数据结构可以支撑这些场景,解决相关问题,本文我们会针对每个场景逐一说明。
4553 149
AI 推理场景的痛点和解决方案
|
10月前
|
运维 算法 计算机视觉
CFAR目标检测程序及原理详解
CFAR目标检测程序及原理详解
|
Linux C++
Linux c/c++之文件拷贝
这篇文章介绍了在Linux环境下使用C/C++进行文件拷贝的两种方法:一种是通过system()函数调用命令行命令cp来拷贝文件,另一种是通过读写文件的方式进行文件拷贝。
588 0
Linux c/c++之文件拷贝
|
安全 编译器 API
Qt实用小技巧:消除警告
Qt实用小技巧:消除警告
854 1