boost::tie()和boost::variant()解说
#include<iostream>
#include<boost/tuple/tuple.hpp>
#include<boost/variant.hpp>
#include<boost/tuple/tuple_io.hpp>
#include<boost/any.hpp>
#include<vector>
#include<iterator>
#include<string>
using namespace std;
boost::tuple<std::string, int> func()
{
return boost::make_tuple("Error message", 2009);
}
std::vector<boost::any> vector1;
struct output : public boost::static_visitor<>
{
template <typename T>
void operator()(T &t) const
{
vector1.push_back(t);
}
};
int main(){
//Boost.Tuple 还提供了一种叫做 Tier 的特殊元组。 Tier 的特殊之处在于它包括的全部元素都是引用类型的。 它能够通过构造函数 boost::tie() 来创建。
int i; char c; double d;
boost::tie(i,c,d) = boost::tuple<int,char,double>(1,'A',0.618);
d = 0.718;
cout<<d<<endl;
//用ignore忽略元素
char c2;
boost::tie(boost::tuples::ignore,c2) = std::make_pair(1,'a');
cout<<c2<<endl;
//boost::tie() 在一定程度上简化了语法。 同一时候, 也能够用作“拆箱”元组。 在接下来的这个样例里, 元组中的各个元素就被非常方便的“拆箱”并直接赋给了其它变量。
std::string errmsg;
int errcode;
boost::tie(errmsg, errcode) = func();
std::cout << errmsg << ": " << errcode << std::endl;
//Boost.Variant 为我们提供了一个定义在 boost/variant.hpp 中的类: boost::variant 。 既然 boost::variant 是一个模板。 你必需要指定至少一个參数。
Variant 所存储的数据类型就由这些參数来指定。
上面的样例就给 v 指定了 double 类型和 char 类型。
注意, 一旦你将一个 int 值赋给了 v, 你的代码将不会编译通过。
//当然。 上面的样例也能够用一个 union 类型来实现, 可是与 union 不同的是: boost::variant 能够储存像 std::string 这种 class 类型的数据。
//boost::apply_visitor() 第一个參数需要传入一个继承自 boost::static_visitor 类型的对象。
这个类必需要重载 operator()() 运算符来处理 boost::variant 每一个可能的类型。
对应的, 样例中的 v 就重载了三次 operator() 来处理三种可能的类型: double, char 和 std::string。
//再细致看代码, 不难发现 boost::static_visitor 是一个模板。 那么。当 operator()() 有返回值的时候, 就必须返回一个模板才行。 假设 operator() 像样例那样没有返回值时。 你就不须要模板了。
//boost::apply_visitor() 的第二个參数是一个 boost::variant 类型的值。
//在使用时, boost::apply_visitor() 会自己主动调用跟第二个參数匹配的 operator()() 。
演示样例程序中的 boost::apply_visitor() 就自己主动调用了三个不同的 operator 第一个是 double 类型的, 第二个是 char 最后一个是 std::string。
//boost::apply_visitor() 的长处不仅仅是“自己主动调用匹配的函数”这一点。 更实用的是, boost::apply_visitor() 会确认是否 boost::variant 中的每一个可能值都定义了对应的函数。 假设你忘记重载了不论什么一个函数, 代码都不会编译通过。
boost::variant<double, char, std::string> v;
v = 3.14;
boost::apply_visitor(output(), v);
v = 'A';
boost::apply_visitor(output(), v);
v = "Hello, world!";
boost::apply_visitor(output(), v);
}
编译后输出:
0.718
a
Error message: 2009
boost::tie()和boost::variant()解说的更多相关文章
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- boost::string or boost::regex
有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...
- [置顶] 编程模仿boost::function和boost::bind
boost::function和boost::bind结合使用是非常强大的,他可以将成员函数和非成员函数绑定对一个对象上,实现了类似C#的委托机制.委托在许多时候可以替代C++里面的继承,实现对象解耦 ...
- 用boost::bind构造boost::coroutine
class TestCoro { ... typedef boost::coroutines::coroutione<void ()> Coro; void CoroFun(Coro::c ...
- 一起学习Boost标准库--Boost.StringAlgorithms库
概述 在未使用Boost库时,使用STL的std::string处理一些字符串时,总是不顺手,特别是当用了C#/Python等语言后trim/split总要封装一个方法来处理.如果没有形成自己的com ...
- boost::unique_lock和boost::lock_guard的区别
lock_guard unique_lock boost::mutex mutex; boost::unique_lock<boost::mutex> lock(mutex); std:: ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- boost::bind和boost::function使用示例
C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的.看如下两段代码: 1) 创建HDFS目录 void hdfs::init() { if (0 == hdfs ...
- boost::function 通过boost::bind调用类成员函数
1. 首先引用boost::function和boost::bind的头文件和库: #include "boost/bind.hpp" #include "boost/f ...
随机推荐
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 使用ViewPager实现android软件使用向导的功能
现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了. 先看下效果图: 首先需要一个布局文件,是FlameLayout组成的,里面包 ...
- Codeforces Round #352 (Div. 2) B. Different is Good 水题
B. Different is Good 题目连接: http://www.codeforces.com/contest/672/problem/B Description A wise man to ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”
报错内容如下: 无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象强制转换为接口类型“Microsoft.Office.Inte ...
- PUSH MESSAGE 云控等交互类测试业务的自动化
针对的业务: 1. PUSH消息,即由云端服务向客户端推送消息 2. MESSAG消息,即用户间消息.用户群消息和聊天室消息 上干货,框架见下图:
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- DISQLite3 - A self-contained, embeddable, zero-configuration SQL database engine for Delphi
DISQLite3 implements a self-contained, embeddable, zero-configuration SQL database engine for Delphi ...
- JTAG Level Translation
http://www.freelabs.com/~whitis/electronics/jtag/ One of the big issues in making a JTAG pod is leve ...
- 解决Fragment每次进入都加载的问题
1.首先了解一下fragment的生命周期 onCreate是指创建该fragment类似于Activity.onCreate,你可以在其中初始化除了view之外的东西,onCreateView是创建 ...