• 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. fckeditor配置

    <!DOCTYPE html> <html > <head> <title>发布</title> <meta name="v ...

  2. golang channel 总结

    1.未初始化的channel读,阻塞 package main import ( "fmt" "time" ) func main() { var ch cha ...

  3. js 关于本地文件的处理

    https://developer.mozilla.org/zh-CN/docs/Web/API/File/Using_files_from_web_applications

  4. CentOS7为firewalld添加开放端口及相关操作

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disabl ...

  5. 一次scrapy失败的提示信息:由于连接方在一段时间后没有正确答复或连接的主机没有反 应,连接尝试失败

    2017-10-31 19:09:26 [scrapy.extensions.logstats] INFO: Crawled 8096 pages (at 67 pages/min), scraped ...

  6. 实验吧—隐写术——WP之 奇妙的音乐

    点击链接下载压缩包,解压后得到:一个图片,一个压缩包 打开图片: 看到海伦.凯勒我们都知道她是一位盲人,而下面黑色和灰色的点点应该就是盲文了,那么我们百度一下对照表 我们将图片里的盲文对照后得到; k ...

  7. HTML中嵌套的子frame如何访问父页面中的函数?

    我解决的办法,在父页面写了个函数,然后在frame页面调用父页面的函数,具体代码如下: 父:function a(){} 子frame:window.parent.a(); 问题迎刃而解 https: ...

  8. 【shell编程】之基础知识-文件包含

    和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...

  9. mysql将某数据库的全部权限赋给某用户,提示1044错误Access denied

    mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; ERROR 1044 (4 ...

  10. auto sudo password in shell

    here is the example how to implement the auto password in shell script. Echo yourpasswordhere | sudo ...