多重ラムダ
using namespace boost::lambda; template<class F, class T> void apply(F f, T v) { f(v); } void func(std::vector<int>& v) { std::for_each(v.begin(), v.end(), _1 *= 2); } void main() { std::vector<int> v; apply(func, boost::ref(v)); }
これを、func 関数を作らずにやるとどうなるか試してみた。
apply( bind(std::for_each<std::vector<int>::iterator, boost::function<void (int&)> >, bind(static_cast<std::vector<int>::iterator (std::vector<int>::*)()>(&std::vector<int>::begin), _1), bind(static_cast<std::vector<int>::iterator (std::vector<int>::*)()>(&std::vector<int>::end), _1), boost::function<void (int&)>(_1 *= 2)), boost::ref(v));
こうなった。
boost::function<> を使ってるのが反則っぽいけど...。
begin, end の時点での _1 は std::vector
ところで、C++0x なら
apply( [](std::vector<int>& v){ std::for_each(v.begin(), v.end(), [](int& n){ n *= 2; }); }, std::ref(v));
こうなるのか...簡単すぎる...。