Boost Variant resembles union. You can store values of different types in a boost::variant.

1.

#include <boost/variant.hpp>
#include <string> int main() {
boost::variant<double, char, std::string> v;
v = 3.14;
v = 'A';
v = "Boost";
}

boost::variant is a template, at least one parameter must be specified. One or more template parameters specify the supported types.

2. accessing values in boost::variant with boost::get()

#include <boost/variant.hpp>
#include <string>
#include <iostream> int main() {
boost::variant<double, char, std::string> v;
v = 3.14;
std::cout << boost::get<double>(v) << std::endl;
v = 'A';
std::cout << boost::get<char>(v) << std::endl;
v = "Boost";
std::cout << boost::get<std::string>(v) << std::endl;
return ;
}

to display the stored values of v, use the free-standing function boost::get().

boost variant的更多相关文章

  1. boost::tie()和boost::variant()解说

    #include<iostream> #include<boost/tuple/tuple.hpp> #include<boost/variant.hpp> #in ...

  2. 让boost.variant支持lambda表达式访问

    前言 之前写个过一篇博客叫<浅谈boost.variant的几种访问方式>,里面讲到了可以通过访问者方式来获取variant的值,但是在重载函数operator()里面只能够获取varia ...

  3. 浅谈boost.variant的几种访问方式

    前言 variant类型在C++14并没有加入,在cppreference网站上可以看到该类型将会在C++17加入,若想在不支持C++17的编译器上使用variant类型,我们可以通过boost的va ...

  4. boost总结之variant

    boost的variant库类似于联合体,但是联合体中只能接受POD类型,但variant中并无此限制,它可以接受任意的类型.   boost::variant <int, std::strin ...

  5. boost数据结构variant

    variant和any有些类似,是一种可变类型,是对C/C++中union概念的增强和扩展:    普通的union只能持有普通数据类型,而不能持有string.vector等复杂类型,而varian ...

  6. boost signal2 slot_base

    先看成员_tracked_objects,从字面上讲是被跟踪的对象,再看,相关函数 bool expired() const,这个函数是检查_tracked_objects是否已经expired.只不 ...

  7. 比特币源码分析--C++11和boost库的应用

    比特币源码分析--C++11和boost库的应用     我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...

  8. (原创)用c++11打造好用的variant(更新)

    关于variant的实现参考我前面的博文,不过这第一个版本还不够完善,主要有这几个问题: 内部的缓冲区是原始的char[],没有考虑内存对齐: 没有visit功能. 没有考虑赋值构造函数的问题,存在隐 ...

  9. (原创)用c++11打造好用的variant

    variant类似于union,它能代表定义的多种类型,允许将不同类型的值赋给它.它的具体类型是在初始化赋值时确定.boost中的variant的基本用法: typedef variant<in ...

随机推荐

  1. Windowed functions can only appear in the SELECT or ORDER BY clauses

    尝试做分页处理 select row_number over (orderby id asc) as rownum,* from table where rownum>=(@page*@page ...

  2. [转]Jenkins HTML报告样式无法显示问题解决

    原文地址: https://vwin.github.io/2018/10/11/Jenkins-HTML%E6%8A%A5%E5%91%8A%E6%A0%B7%E5%BC%8F%E6%97%A0%E6 ...

  3. MapReduce(3): Partitioner, Combiner and Shuffling

    Partitioner: Partitioning and Combining take place between Map and Reduce phases. It is to club the ...

  4. 编程语言-Python-GUI

    PyQt5 import sys from PyQt5 import QtWidgets,QtCore app = QtWidgets.QApplication(sys.argv) widget = ...

  5. Postman初接触

    https://www.getpostman.com/docs/postman/launching_postman/installation_and_updates

  6. 在Linux下将HTML文件转换成PDF文件

    今天要写一个上交的作业,本来是想用Office Word来写的,但是,我的Office貌似不能用了,但是,Linux下的LibreOffice写出的文档,在打印的时候是经常出现乱码的.所以,后来想到可 ...

  7. 【洛谷p1036】选数

    (一定要声明我太蒟了,这个题扣了一上午……) 算法标签: …… dfs真的不是我所擅长的qwq,这道题的思路其实很简单,就是先dfs搜索所有可能的和,然后判断是不是质数.说着好说,然鹅并不好写: 第一 ...

  8. 爬虫之requests模块的使用

    requests模块 概念:基于网络请求的模块 作用:用来模拟浏览器发请求,从而实现爬虫 环境安装:pip install requests 编码流程: 指定url 发起请求 获取响应数据 持久化存储 ...

  9. proxool连接池配置方法

    proxool.properties: jdbc-1.proxool.alias=test #jdbc-1.proxool.driver-class=com.mysql.jdbc.Driver #jd ...

  10. python学习三十八天常用内置函数分类汇总

    python给我们提供丰富的内置函数,不用去写函数体,直接调用就可以运行,很方便快速给我提供开发所需要的函数. 1,查内存地址 id() 变量的内存地址 id() 2,输入输出 input()  pr ...