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. php7新特性的理解和比较

    1. null合并运算符(??) ??语法: 如果变量存在且值不为NULL,它就会返回自身的值,否则返回它的第二个操作数. //php7以前 if判断 if(empty($_GET['param']) ...

  2. science_action

    w import random import pprint import math import matplotlib.pyplot as plt def gen_random(magnify_=10 ...

  3. SqlSession 内部运行

    <深入浅出MyBatis技术原理与实战>p150页 SqlSession内部运行图 四大对象在流程中的操作. 1.准备sql.StatementHandler 的prepare方法进行sq ...

  4. Dapper(一) 简介和性能

    Dapper的简介 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.Dapper只有一个代码文件,完全开源,你 ...

  5. 从零搭建一个Redis服务

    前言 自己在搭建redis服务的时候碰到一些问题,好多人只告诉你怎么成功搭建,但是并没有整理过程中遇到的问题,所有楼主就花了点时间来整理下. linux环境安装redis 安装中的碰到的问题和解决办法 ...

  6. poj2431Expedition

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...

  7. Go-Mutex互斥量

    先来看一段go1.12.5中Mutex的源码: // Copyright 2009 The Go Authors. All rights reserved. // Use of this source ...

  8. (转载)Solr4.x在Tomcat下的部署

    Step1 下载安装包: 下载最新版本安装包 点击此处下载Tomcat    点击此处下载Solr Step2 解压: 解压Tomcat和Solr Step3 拷贝War包: 拷贝\solr-4.x\ ...

  9. 远程连接不上centos的mysql的解决方法

    1.防火墙没有开放3306端口 centos 有两种防火墙 FirewallD和iptables防火墙 centos7 使用的是FirewallD防火墙. 1.FirewallD防火墙开放3306端口 ...

  10. SQL 一次插入多次数据

    数据插入 INSERT INTO 表名称 VALUES (值1, 值2,....) 指定所要插入数据的列 INSERT INTO table_name (列1, 列2,...) VALUES (值1, ...