boost::any是个容器,只包容一个元素,而且可以用任意类别构造。因为其吧类型归一,所以就适合用STL容器来容纳。
这让人想起Lua里面的变量,比any更加宽泛。连函数都可以包括。内部可以“就地”产生一个函数来返回。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <boost/any.hpp>
#include <vector>
#include <string>
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
struct propty
{
std::string _name;
boost::any _val;
propty (std::string name, boost::any val) :_name (name), _val (val) {};
};
typedef std::vector<propty> proptys;
template <typename T>
bool contains (const boost::any& a)
{
return typeid(T) == a.type ( );
}
struct anyReader : std::unary_function<void,propty>
{
void operator()(const propty& p)
{
typedef const char* cchar;
std::cout << p._name << " ";
if(const char* const

本文介绍了boost::any,这是一个能存储任意类型的C++容器。它将不同类型的元素归一化,使得可以轻松地在STL容器中使用。通过any_cast和type()成员函数,可以方便地获取和转换存储的类型,尤其适用于处理多类型属性的场景。相比于C的union,boost::any提供了更简洁和强大的解决方案,并且结合assign库可以进一步增强其功能。
最低0.47元/天 解锁文章
982

被折叠的 条评论
为什么被折叠?



