boost的variant库类似于联合体,但是联合体中只能接受POD类型,但variant中并无此限制,它可以接受任意的类型.
 
boost::variant <int, std::string , double> u;
u = 4;
int i = boost:: get<int >(u);
std::cout << "int : " << i << std ::endl;
 
u = "hello world!" ;
std::string s = boost::get <std:: string>(u );
std::cout << "string : " << s << std ::endl;
 
u = 1.2;
double d = boost:: get<double >(u);
std::cout << "double : " << d << std ::endl;
 
输出:
int : 4
string : hello world!
double : 1.2
 
要获取variant中的值时,一定要知道当前类型,并由get操作进行获取相应的值,如果转型失败,会抛出bad_get异常,get操作时运行时检测,我们可以通过检测get返回值来进行判断转型是否成功如下:
 
void printer (boost:: variant<int , std:: string, double >& u)
{
    if (int *pi = boost::get <int>(& u))
    {
        std::cout << "int : " << * pi << std ::endl;
    }
    else if (std:: string *ps = boost:: get<std ::string>(& u))
    {
        std::cout << "string : " << * ps << std ::endl;
    }
    else if (double * pd = boost ::get< double>(&u ))
    {
        std::cout << "double : " << * pd << std ::endl;
    }
}
 
通过printer函数中我们为get传进去的是variant的地址,返回如果是NULL的话则转型失败,进行下一个转型操作,依次来判断数据类型.可以看到通过get操作不仅需要知道需要转型的类型,而且还要在运行时判断转型是否成功,boost提供了编译时类型检测的安全访问方式,通过apply_visitor进行访问.
 
class printer_visitor : public boost::static_visitor <void>
{
public:
    void operator ()(int i) const
    {
        std::cout << "int : " << i << std ::endl;
    }
 
    void operator ()(std:: string& s ) const
    {
        std::cout << "string : " << s << std ::endl;
    }
 
    void operator ()(double d) const
    {
        std::cout << "double : " << d << std ::endl;
    }
};
int main ()
{
    boost::variant <int, std::string , double> u;
    printer_visitor visitor ;
    u = 4;
    boost::apply_visitor (visitor, u);
    u = "hello world!" ;
    boost::apply_visitor (visitor, u);
    u = 1.2;
    boost::apply_visitor (visitor, u);
}
 
我们定义一个variant时,如果我们没有给它赋任何值时,是不是他就是空呢?boost文档中"Never-Empty" Guarantee.也就是即使我们没有初始化,他也不会是空,默认的类型是我们模板参数的第一个,上例中默认的类型也就是int,值是0.
 
我们还可以将variant作为容器的元素例如:
 
std::vector <boost:: variant<int , std:: string, double > > v;
v.push_back (4);
v.push_back ("hello world!");
v.push_back (1.2);
std::for_each (v. begin(), v .end(), boost::apply_visitor (printer_visitor()));

boost总结之variant的更多相关文章

  1. 实现一个 Variant

    很多时候我们希望能够用一个变量来保存和操作不同类型的数据(比如解析文本创建 AST 时保存不同类型的结点),这种需求可以通过继承来满足,但继承意味着得使用指针或引用,除了麻烦和可能引起的效率问题,该做 ...

  2. 64位win7下安装Boost 1.59.0 + boost.python 1.59.0 + gccxml + pygccxml + pyplusplus(py++)

    由于安装过程中实在是出现了N多问题,所以不得不专门写个帖子来记录一下这破东西在Win7下的安装过程,避免以后还要再用的时候踩坑. 1.Boost简介 Boost库是一个可移植.提供源代码的C++库,作 ...

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

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

  4. 转:MFC之COleVariant

    COleVariant 本质上是一个枚举,用同一种类型来表达不同的子类型.如同boost中的variant. 例子 COleVariant var(3.6f); float v = var.fltVa ...

  5. Boost--variant (C++中的union)

    union联合体类型的问题 只能用于内部类型,这使得union在C++中几乎没有用 所以boost提供了variant,相当于是C++中的union #include "boost/vari ...

  6. COleVariant类

    COleVariant本质上是一个枚举,用同一种类型来表达不同的子类型.如同boost中的variant. COLeVariant类是对VARIANT结构的封装. VARIANT结构包含两部分.其一是 ...

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

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

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

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

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

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

随机推荐

  1. evaluateScript--evaluatePopoverScript--区别

    appcan.window.evaluateScript({})              //window.open()页面之间使用 appcan.window.evaluatePopoverScr ...

  2. 过滤部分错误信息,不输出到stderr

    cmd 2>/tmp/stderr.log cat /tmp/stderr.log | grep -v “要过滤信息的关键字” >&2 rm /tmp/stderr.log

  3. Dede 列表页 缩略图 有显示无则不显示

    [field:array runphp='yes']@me = (strpos(@me['litpic'],'defaultpic') ? "" : "<div c ...

  4. VC++2010添加菜单

    1  资源视图下面右键添加资源 选择menu 2  创建你想要的menu 3  找到CDialog::OnInitDialog();在后面添加代码. CMenu menu; menu.LoadMenu ...

  5. 005.ClearStoredGroups方法

    Delphi procedure ClearStoredGroups; 类型:procedure 可见性:protected 所在单元:System.RegularExpressionsCore 父类 ...

  6. SQL学习_查询重复数据和连接多个表数据的方法

    进行数据库测试时需要根据不同场景查询数据,以便验证发现的问题是否为脏数据引起的.记录一下最近常用的查询方法: 1. 查询表中重复数据(id不同,多个字段值相同) select P1.* from pr ...

  7. centos系统python升级2.7.3

    首先下载源tar包 可利用linux自带下载工具wget下载,如下所示: wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz 下载 ...

  8. 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ...

  9. java JDBC操作MySQL数据库

    一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...

  10. 说说对C语言指针的理解

    指针困扰了一些学习编程的人,或许你的老师会告诉你,指针比较难理解. 我当时被老师的话唬住所以学习指针那章的时候都没心情听课.(说得像讲别的内容时我听了似的,开玩笑) 导致了学习链表的时候各种卧槽. * ...