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. Newtonsoft.Json随手记

    private static Newtonsoft.Json.JsonSerializerSettings CreateSettings(string dateFormat) { Newtonsoft ...

  2. debian 学习记录-5

    后裔排名 1 Debian - 1292 Fedora - 633 Knoppix - 50 (Knoppix本身是Debian后裔) Debian4 SuSE - 28 Debian,由Ian Mu ...

  3. Winform TreeView控件技巧

    在开发的时候经常使用treeview控件来显示组织结构啊,目录结构啊,通常会结合属性checkedboxs,来做选中,取消的操作下面是一个选中,取消的小例子,选中节点的时候,如果节点存在子节点,可以选 ...

  4. Linux procfs详解

    1.0 proc文件系统总览在类Unix系统中体现了一种良好的抽象哲学,就是几乎所有的数据实体都被抽象成一个统一的接口--文件来看待,这样我们就可以用一些简单的基本工具完成大量复杂的操作.在Linux ...

  5. 基于等待队列及poll机制的按键驱动代码分析和测试代码

    按键驱动分析: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> ...

  6. 支持阻塞操作和轮询操作的globalfifo设备驱动代码分析以及测试代码

    #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #include ...

  7. Xilium.CefGlue利用XHR实现Js调用c#方法

    防外链 博客园原文地址在这里http://www.cnblogs.com/shen6041/p/3442499.html 引 Xilium CefGlue是个不错的cef扩展工程,托管地址在这里 ht ...

  8. 面向站长和网站管理员的Web缓存加速指南

    详细了解HTTP缓存控制及为什么要缓存. 英文版: http://www.mnot.net/cache_docs/ 中文版:http://www.chedong.com/tech/cache_docs ...

  9. 成为Java GC专家(3)—如何优化Java垃圾回收机制

    为什么需要优化GC 或者说的更确切一些,对于基于Java的服务,是否有必要优化GC?应该说,对于所有的基于Java的服务,并不总是需要进行GC优化,但前提是所运行的基于Java的系统,包含了如下参数或 ...

  10. online web design tool

    https://webflow.com/ http://css-tricks.com/snippets/ https://www.fluidui.com/demos