#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/assert.hpp>
#include <boost/format.hpp>
#define PRINT(xxx) ( std::cout << boost::format("%-20s = ") % (#xxx) << (xxx) << std::endl)
//检测一个any是否能转换成某种类型,RTTI
template<typename T>
bool can_cast_to(const boost::any& a){
return a.type() == typeid(T);
}
//获取any内部对象的引用,可以用来修改对象的值
template<typename T>
T& get_ref(boost::any& a){
BOOST_ASSERT_MSG(can_cast_to<T>(a), "any类型转换错误!!!");
return boost::any_cast<T&>(a);
}
//获取原对象的指针
template<typename T>
T* get_ptr(boost::any& a){
BOOST_ASSERT_MSG(can_cast_to<T>(a), "any类型转换错误!!!");
return boost::any_cast<T>(&a);
}
int main(int ar
boost::any基本用法
最新推荐文章于 2024-08-30 16:34:15 发布