してみました。
MinGW環境への導入がよくわかんなかったので、ドキュメントを大して読まずに、
"boost_1_45_0\boost"を、"mingw\lib\gcc\mingw32\4.5.0\include\c++" にboostディレクトリごとに放り込んだら動いてくれました。
コレだけで導入と言えるか怪しい・・・問題が起きたらまぁその時はその時で・・・
便利なライブラリを使うためにBoostを使う人が多そうですが、
C++の機能を生かしたライブラリがたくさんあるので、ライブラリのコードを読むだけでも発見があって楽しいと思います。
(ただC++がわからないので、それらのコードが魔術のようにも見えてますが:P)
早速boost::anyとboost::shared_ptrを使って簡単なサンプルを1個書いてみました。
#include <iostream> #include <vector> #include <boost/any.hpp> #include <boost/shared_ptr.hpp> class Test{ public: ~Test(){ std::cout << "destructor" << std::endl; } const std::string toString(){return "Test::toString";} }; const std::string checktype(const boost::any & a){ return a.type() == typeid(int) ? "int" : a.type() == typeid(double) ? "double" : a.type() == typeid(std::string) ? "std::string" : a.type() == typeid(boost::shared_ptr<Test>) ? "boost::shared_ptr<Test>" : "Unknown" ; } int main(){ std::vector<boost::any> x; x.push_back(1); x.push_back(3.1415926); x.push_back(std::string("moji moji")); x.push_back( boost::shared_ptr<Test>(new Test()) ); std::cout << boost::any_cast<int>(x[0]) << std::endl; std::cout << "checktype(x[0]): " << checktype(x[0]) << std::endl; std::cout << boost::any_cast<double>(x[1]) << std::endl; std::cout << "checktype(x[1]): " << checktype(x[1]) << std::endl; std::cout << boost::any_cast<std::string>(x[2]) << std::endl; std::cout << "checktype(x[2]): " << checktype(x[2]) << std::endl; std::cout << boost::any_cast< boost::shared_ptr<Test> >(x[3]).get()->toString() << std::endl; std::cout << "checktype(x[3]): " << checktype(x[3]) << std::endl; }
1
checktype(x[0]): int
3.14159
checktype(x[1]): double
moji moji
checktype(x[2]): std::string
Test::toString
checktype(x[3]): boost::shared_ptr
destructor
boost::any_castを使って格納した値を変換して取得する必要がありますが、
ほぼ何でも代入する事ができる事がわかりました。
newで確保した値も、boost::shared_ptrによって最後にデストラクタをちゃんと呼んでくれています。