Boost C++ Libraryのお勉強その7
Boost.Enable_If
普通に使うとテンプレート関数あらゆる型を取るので、 それを制御するための仕組み。
ぱっと見使い方がわかりずらくて、まずenable_ifは引数に書くけどこれは宣言するだけで関数内では使わず、あくまでテンプレートの実体化の為に処理系が使う。(多分)
そして対になるdisable_ifも必要。以下の例だとsignedとsigned以外にするため、単純にfunc(T arg)とするのでなく明示的にdisable_ifを書かないとsignedの実体化で両方の関数が該当してしまう。
#include <iostream> #include <boost/type_traits.hpp> #include <boost/utility/enable_if.hpp> template < typename T > void func( T arg, typename boost::enable_if < boost::is_signed < T > >::type* = 0) { std::cout << "signed : " << arg << std::endl; } template < typename T > void func( T arg, typename boost::disable_if < boost::is_signed < T > >::type* = 0) { std::cout << "not signed : " << arg << std::endl; } int main(void) { func(1); func(1.0); return 0; }
signed : 1 not signed : 1
Boost.Filesystem
便利なファイル操作とか色々。リンク時にboost_filesystemをくっつける必要がある。
#include <iostream> #include <boost/filesystem.hpp> int main(void) { std::cout << boost::filesystem::exists(".") << std::endl; return 0; }
1
Boost.Foreach
コンテナの全要素をループで処理する為のforeach。結構便利。
#include <string> #include <iostream> #include <boost/foreach.hpp> int main(void) { std::string hell_world("Hello, world!"); BOOST_FOREACH(char c, hell_world) { std::cout << c << std::endl; } return 0; }
H e l l o , w o r l d !