* 该 ValueType 还有可选的 Assignable 的。而对于很强的异常安全保证的话,就需要所有形式的 Assignment;
* 析构函数 upholds the no-throw exception-safety guarantee.
namespace boost { |
3,any 类的摘要
class any { |
3,示例:
#include <vector>
#include <string>
#include <iostream>
#include <boost/any.hpp>
using boost::any_cast;
typedef std::vector<boost::any> many;
// 向容器中插入 int 型值
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
// 向容器中插入 string 型值
void append_string(many & values, const std::string & value)
{
values.push_back(value);
}
// 向容器中插入 const char* 型值
void append_char_ptr(many & values, const char * value)
{
values.push_back(value);
}
// 向容器中插入 const boost::any& 型值
void append_any(many & values, const boost::any & value)
{
values.push_back(value);
}
// 什么都没有,就是一个空值
void append_nothing(many & values)
{
values.push_back(boost::any());
}
// ----- 下面也演示了一些与 predicates 相关方法
// 是否为空值
bool is_empty(const boost::any & operand)
{
return operand.empty();
}
// 查看类型值
bool is_int(const boost::any & operand)
{
return operand.type() == typeid(int);
}
// 注意指针类型转型的异常处理
bool is_char_ptr(const boost::any & operand)
{
try
{
any_cast<const char *>(operand);
return true;
}
catch(const boost::bad_any_cast &)
{
return false;
}
}
// 对于其他类,直接使用 any 转型
bool is_string(const boost::any & operand)
{
return any_cast<std::string>(&operand);
}
// 统计每种类型的个数
void count_all(many & values, std::ostream & out)
{
out << "#empty == "
<< std::count_if(values.begin(), values.end(), is_empty) << std::endl;
out << "#int == "
<< std::count_if(values.begin(), values.end(), is_int) << std::endl;
out << "#const char * == "
<< std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl;
out << "#string == "
<< std::count_if(values.begin(), values.end(), is_string