• union联合体类型的问题

    • 只能用于内部类型,这使得union在C++中几乎没有用
  • 所以boost提供了variant,相当于是C++中的union

#include "boost/variant.hpp"
#include <vector>
#include <iostream>
#include <array>
#include <string> using namespace std; class DoubleVisitor : public boost::static_visitor<> {
public:
void operator() (int& i) const {
i += i;
}
void operator() (string& str) const {
str += str;
}
}; void Double( boost::variant<int, string> u) {
} int main()
{
// C union:
union {int i; float f;} u; // u要么包含一个int,要么包含一个float
u.i = 34;
cout << u.i << endl;
u.f = 2.3; // u.i被覆盖
cout << u.i << endl; // 输出无效的结果
// 问题:只支持内部类型 //union {int i; string s;} u; // 比如string就编译不了 // variant是C++中的union
boost::variant<int, string> u1, u2;
u1 = 2;
u2 = "Hello";
cout << u1 << endl;
cout << u2 << endl;
//u1 = u1 * 2; // variant类型没有重载*,不能直接进行操作
u1 = boost::get<int>(u1) * 2; // 使用get() 返回一个int的引用
// 如果是variant<int*, string>, get()返回int的指针 cout << boost::get<int>(u1) << endl; // output: 64
//cout << boost::get<string>(u1) << endl; // 崩。 variant是带鉴别能力的union
// 如果检索失败, get() 返回空指针或者抛出一个异常: bad_get
u1 = "good"; // u1 变成一个string
u1 = 32; // u1 变成一个int // variant永远不会为空
boost::variant<int, string> u3;
cout << boost::get<int>(u3) << endl; // 使用get的问题是我们并不总是知道保存在variant中的类型是什么 //这个时候可以使用visitor
// 定义仿函数,为variant的不同类型重载函数调用运算符
boost::apply_visitor( DoubleVisitor(), u1 );
cout << boost::get<int>(u1) << endl; // output: 128 boost::apply_visitor( DoubleVisitor(), u2 );
cout << boost::get<string>(u2) << endl; // output: HelloHello std::vector< boost::variant<int, string> > arr;
arr.push_back("good");
arr.push_back(25);
arr.push_back("bad");
for (auto x : arr) {
boost::apply_visitor( DoubleVisitor(), x); //会根据variant中存的不同类型,进行不同的操作
}
}

Boost--variant (C++中的union)的更多相关文章

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

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

  2. boost variant

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

  3. C++中使用union的几点思考(转)

    C++中使用union的几点思考 大卫注:这段时间整理旧资料,看到一些文章,虽然讲的都是些小问题,不大可能用到,但也算是一个知识点,特整理出来与大家共享.与此相关的那篇文章的作者的有些理解是错误的,我 ...

  4. Ms SQLServer中的Union和Union All的使用方法和区别

    Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...

  5. Oracle中的Union、Union All、Intersect、Minus

    Oracle中的Union.Union All.Intersect.Minus  众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...

  6. golang 中的 sizeof 以及 golang中的 union

    golang 中的 sizeof: 1: int(unsafe.Sizeof(uint32(0))) 2: int(reflect.TypeOf(uint32(0)).Size()) golang中的 ...

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

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

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

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

  9. mysql中的union操作(整理)

    mysql中的union操作(整理) 一.总结 一句话总结: union两侧的字段数和字段类型要是一样的 union可以接多个 orderby和排序可以在最后的union组合之后 1.union简单实 ...

随机推荐

  1. [Data Structure] An Algorithm for Matching Delimiters

    An important task when processing arithmetic expressions is to mach delimiters. We can use Stack to ...

  2. thrift使用案例

    参考资料:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ 首先是定义thrift IDL接口,如下(SunTelTc.thri ...

  3. indexedDB为何物

    https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 在前一个阶段的工作中,项目组要开发一个平台,为了做出更好的用户体验,实现快 ...

  4. Win-Lin双系统重装Windows找回Linux启动

    第一系统Windows,第二系统Linux:Ubuntu18.10: 1. 重新安装Windows系统后,使用Ubuntu的安装光盘,或启动U盘启动电脑:2. 选择:Try Ubuntu ;3. 进入 ...

  5. 《DSP using MATLAB》Problem5.23

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% O ...

  6. exception in thread "http-apr-80-exec-24" java.lang.OutOfMemoryError:PermGen...

    今天客户说项目访问不了了,我急忙看了下告警,发现上报:“exception in thread "http-apr-80-exec-24" java.lang.OutOfMemor ...

  7. Mybatis(七)-- LRU LFU 算法

    这篇博客主要介绍LRU LFU 算法,因为在Mybatis的缓存中会用到,所以放到这个系列中了.此外,这是我翻译的一篇文章,觉得原文已经写的很好了,所以就直接翻译一下,留作知识整理. 英文原文出处如下 ...

  8. top-adx-apps

    中国地区top adx和流量平台 12719 unity ios 12001 mopub ios 6599 unity android 5277 mobfox ios 3855 mopub andro ...

  9. day6 python学习

    ---恢复内容开始--- 今日讲课内容: 1.  新内容: 字典  1.字典有无序性,没有顺序,2字典的键:key必须是可哈希的.可哈希表示key必须是不可变类型,如:数字.字符串.元组.不可变的,字 ...

  10. Optimizing Docker Images for Rust Projects

    转自:http://whitfin.io/speeding-up-rust-docker-builds/ This post will be the first of several addressi ...