The following noddy test code:
#include <iostream>
#include <list>
#include <boost/any.hpp>
#include <boost/foreach.hpp>
#include <typeinfo.h>
using boost::any_cast;
using std::cout;
using std::cerr;
typedef std::list<boost::any> many;
template <typename T>
inline bool is_any(const boost::any& op)
{
return (op.type() == typeid(T));
}
int main()
{
many theStrangeList;
theStrangeList.push_back("Can you really...");
theStrangeList.push_back(std::string ("do random types in 1 container?"));
theStrangeList.push_back(6.359);
theStrangeList.push_back(7);
BOOST_FOREACH(boost::any a, theStrangeList)
{
try
{
if (is_any<const char*>(a))
{
cout << any_cast<const char*>(a) << '\n';
}
else if (is_any<std::string>(a))
{
cout << any_cast<std::string>(a) << '\n';
}
else if (is_any<double>(a))
{
cout << "double = " << any_cast<double>(a) << '\n';
}
}
catch (const boost::bad_any_cast& e)
{
cerr << e.what();
cerr << "\n";
}
}
return 0;
}
Compiles and works fine using Sun's CC compiler and default settings. However when using g++ I get the following :
$ g++ -I$BOOST_ROOT -o myany myany.cpp
myany.cpp:5:22: typeinfo.h: No such file or directory
/ilx/boost_1_41_0/boost/any.hpp: In constructor `boost::any::holder<ValueType>::holder(const ValueType&) [with ValueType = char[18]]':
/ilx/boost_1_41_0/boost/any.hpp:47: instantiated from `boost::any::any(const ValueType&) [with ValueType = char[18]]'
myany.cpp:21: instantiated from here
/ilx/boost_1_41_0/boost/any.hpp:122: error: ISO C++ forbids assignment of arrays
This is g++ version 3.4.3, so it might be different on a 4.x version, I'll try it later. Is this the reason why there isn't a 'is_any' template included with boost any, or is it a compiler bug?
I get the same result if I remove the template, as you would expect with an inlined function.