boost::any 用法
boost::any可以存放任何类型的C++类型,也可以是用户自定义的类型。非常方便,可以很方便的满足在运行过程中判断数据类型,从而进行相关的操作。
函数原型:
// In header: <boost/any.hpp> class any {
public:
// construct/copy/destruct
any();
any(const any &);
template<typename ValueType> any(const ValueType &);
any & operator=(const any &);
template<typename ValueType> any & operator=(const ValueType &);
~any(); // modifiers
any & swap(any &); // queries
bool empty() const;
const std::type_info & type() const;
};
成员函数说明:
Boost::Any的实现比较简单,Any拥有一个模版构造函数,这使他可以接受任何类型的对象。真正的变量内容被封装在嵌套类类型的成员变量中,并且在嵌套类中使用typeid来记录真正的类型信息。
1) any& swap(any& other);交换存在两个 any 对象中的值。
2) any& operator=(const any& other);如果any实例非空,则丢弃所存放的值,并存入other值的拷贝。
3)template<typename ValueType> any& operator=(const ValueType& value);如果any实例非空,则丢弃所存放的值,并存入 value 的一份拷贝,value可以是任意符合any要求的类型。
4) bool empty() const;给出any实例当前是否有值,不管是什么值。因而,当any持有一个指针时,即使该指针值为空,则 empty也返回 false 。
5) const std::type_info& type() const;给出所存值的类型。如果 any 为空,则类型为 void.
6) any_cast()将any类型转化为真实的类型,如果是指针返回的可能是空指针,如果非指针,则可能会抛出异常。
三 实例
1)简单实例以及调用常用的成员函数:
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "boost/any.hpp" class A
{
public:
void some_function()
{
std::cout << "A::some_function()\n";
}
};
class B
{
public:
void some_function()
{
std::cout << "B::some_function()\n";
}
}; void print_any(boost::any& a)
{
if (A* pA=boost::any_cast<A>(&a))
{
pA->some_function();
}
else if (B* pB=boost::any_cast<B>(&a))
{
pB->some_function();
}
else
{
try
{
std::cout << boost::any_cast<std::string>(a) << '\n';
}
catch(boost::bad_any_cast&)
{
std::cout << "Oops!\n";
}
}
} int main()
{
std::cout << "Example of using any.\n\n";
std::vector<boost::any> store_anything;
store_anything.push_back(A());
store_anything.push_back(B());
// 我们再来,再加一些别的东西
store_anything.push_back(std::string("This is fantastic! "));
store_anything.push_back();
store_anything.push_back(std::make_pair(true, 7.92));
std::for_each( store_anything.begin(), store_anything.end(), print_any); std::cout << "Example of using any member functions\n\n";
boost::any a1();
boost::any a2(std::string(""));
boost::any a3;
std::cout << "a3 is ";
if (!a3.empty())
{
std::cout << "not empty\n ";
}
std::cout << "empty\n";
a1.swap(a2);
try
{
std::string s=boost::any_cast<std::string>(a1);
std::cout << "a1 contains a string: " << s << "\n";
}
catch(boost::bad_any_cast& e)
{
std::cout << "I guess a1 doesn't contain a string!\n";
}
if (int* p=boost::any_cast<int>(&a2))
{
std::cout << "a2 seems to have swapped contents with a1: " << *p << "\n";
}
else
{
std::cout << "Nope, no int in a2\n";
}
if (typeid(int)==a2.type())
{
std::cout << "a2's type_info equals the type_info of int\n";
} } )解决类似与map但是可以映射到各种不同的类型的问题: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "boost/any.hpp"
class property
{
boost::any value_;
std::string name_;
public:
property(const std::string& name,const boost::any& value)
: name_(name),value_(value) {}
std::string name() const { return name_; }
boost::any& value() { return value_; }
friend bool operator<(const property& lhs, const property& rhs)
{return lhs.name_<rhs.name_;}
};
void print_names(const property& p)
{
std::cout << p.name() << "\n";
}
int main()
{
std::cout << "Example of using any for storing properties.\n";
std::vector<property> properties;
properties.push_back( property("B", ));
properties.push_back( property("A", std::string("Thirty something")));
properties.push_back(property("C", 3.1415));
std::sort(properties.begin(),properties.end());
std::for_each(properties.begin(), properties.end(), print_names);
std::cout << "\n";
std::cout << boost::any_cast<std::string>(properties[].value()) << "\n";
std::cout << boost::any_cast<int>(properties[].value()) << "\n";
std::cout << boost::any_cast<double>(properties[].value()) << "\n";
}
四 注意
1)Any中如果是指针,要注意指针的最后的释放,最好使用shared_ptr来管理。
2)Any中如果是指针,如果Any.isempty()返回false,但是any所包含的指针仍可能是无效的。
3)Any中如果是指针,则在调用any_cast()转化的过程中不会抛出异常,但是如果是一般变量或引用的话,类型不正确会抛出boost::bad_any_cast异常。
五 参考
1)Beyond the C++ Standard Library: An Introduction to Boost
2)boost在线document
参考:http://www.cppblog.com/mzty/archive/2007/08/16/30156.html
any类实现:
boost::any类并不是一个模板类,这可以大大的方便上层应用的使用,它会自动化的类型转换。核心就是any类中,包含一个模板类holder的基类placeholder指针,而placeholder却不是模板类,这样就可以被any类使用了。
具体的如下面所示:
http://www.cnblogs.com/wuerping/articles/116414.html
更多:http://blog.csdn.net/hityct1/article/details/4186962
boost::any 用法的更多相关文章
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- [转] boost::function用法详解
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...
- boost::thread用法
最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...
- boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- boost::function用法详解
要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...
- Boost::split用法详解
工程中使用boost库:(设定vs2010环境)在Library files加上 D:\boost\boost_1_46_0\bin\vc10\lib在Include files加上 D:\boost ...
- boost::algorithm用法详解之字符串关系判断
http://blog.csdn.net/qingzai_/article/details/44417937 下面先列举几个常用的: #define i_end_with boost::iends_w ...
- boost::fucntion 用法详解
转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...
- [转] boost:lexical_cast用法
转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/ 一.lexical_cast的作用lexical_cast使用统一的接 ...
随机推荐
- 一个线程可以拿到多个锁标记,一个对象最多只能将monitor给一个线程
当用Synchronized修饰某个方法的时候,表示该方法都对当前对象加锁. 给方法加Synchronized和用Synchronized修饰对象的效果是一致的. 一个线程可以拿到多个锁标记,一个对象 ...
- iOS音频播放 (三):AudioFileStream 转
原文出处 :http://msching.github.io/blog/2014/07/09/audio-in-ios-3/ 前言 本来说好是要在第三篇中讲AudioFileStream和AudioQ ...
- 转载:基于Redis实现分布式锁
转载:基于Redis实现分布式锁 ,出处: http://blog.csdn.net/ugg/article/details/41894947 背景在很多互联网产品应用中,有些场景需要加锁处理,比如 ...
- mac zsh选择到行首的快捷键
Mac OS X 下zsh切换窗口的快捷键:Shift-Command-←. 移动到当前命令行的行首,使用快捷键[Ctrl][A].移动到当前命令行的行尾,使用快捷键[Ctrl[E].
- ThinkPHP跳转与重定向的区别
跳转: 浏览器认为: 当前URL请求成功, 重新请求新的URL. 浏览器会 记录当前的URL 和 新的URL 在请求历史记录中. 回退, 是可以回退到, 当前的URL上的. (无论 success, ...
- RabbitMQ用户角色及权限控制 -2
1.RabbitMQ的用户角色分类: none.management.policymaker.monitoring.administrator none 不能访问 management plugin ...
- 20 个常用的 CSS 技巧
1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate { filter: grayscale(100%); -webkit-filter: g ...
- 自己动手写CPU之第九阶段(2)——载入存储指令说明2(lwl、lwr)
将陆续上传新书<自己动手写CPU>.今天是第38篇,我尽量每周四篇,可是近期已经非常久没有实现这个目标了.一直都有事,不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9%A9 ...
- MD5骨骼动画模型加载
前面我们分析了静态模型OBJ格式,桢动画模型MD2,这篇主要分析骨骼动画MD5的一些概念并且实现. 混合桢动画有计算简单,容易实现等优点,但是在需要比较细致的效果时,则需要更多的关键桢,每桢都添加相同 ...
- ViewPageIndicator
关于我的总结 1.写一个类extend HorizontalScrollView 实现ViewPagerIndicator,这样写index需要自己写有点啰嗦 2.自定义一个类extends Hori ...