• 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. Python urllib.quote

    转: 编码:urllib.quote(string[, safe]),除了三个符号“_.-”外,将所有符号编码,后面的参数safe是不编码的字符, 使用的时候如果不设置的话,会将斜杠,冒号,等号,问号 ...

  2. 格式化json

    格式化输出JSON var obj = {"args0":{"asynTarget":false,"atomicList":[{" ...

  3. ::before和::after伪元素

    伪元素的意思就是,元素不是在DOM中生成的,而是在浏览器渲染CSS的时候画上去的,所以在浏览器查看元素上是看不到伪元素的HTML结构的. before 和 after 顾名思义就是附着在元素前后的伪元 ...

  4. Quick Noodle Physics in Blender Tutorial

    https://www.youtube.com/watch?v=Lg7jxAMs60QQuick Noodle Physics in Blender Tutorial 新增平面Plane作为地面; 新 ...

  5. golang-build-error

    工程中同时有两个main文件,编译的时候提示: go build proxy/proxy.go pb/anti_spam.pb.go::: cannot find package "_/Us ...

  6. 使用C语言简单模拟Linux的cat程序

    先给出源码 //fileio.c #include<stdio.h> #include<stdlib.h> #include<fcntl.h> void print ...

  7. firefox一搜索就提示是否进入***网站和取消占地方的标题栏

    来看一下这个蛋疼的提示 每次都要手动关闭.后来在网上看到一个解决方法 解决方法: 地址栏输入about:config回车进入设置, 去掉警告那个勾 点击确定,进入配置页 搜索 取消最上面方人的fire ...

  8. java中的数据结构

    1.链表的使用 使用时需要import java.util.List 和 java.util.ArrayList //返回list中的元素个数 int size(); //判断list中是否包含元素, ...

  9. MySQL Innodb Engine -- 文件格式(innodb_file_format)

    ======================================================== 在InnoDB 1.0.x版本之前,InnoDB 存储引擎提供了 Compact 和 ...

  10. Object Relational Mapping(ORM)

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...