Boost PhoenixとBoost Lambdaは何が違うのか?
個人的に調べた限りでは、「ほぼ同じ」である。
Boost PhoenixはBoost Spiritのために開発されている(サブライブラリ)。
1#include <boost/phoenix/phoenix.hpp>
2#include <vector>
3#include <algorithm>
4#include <iostream>
5
6bool is_odd(int i) { return i % 2 == 1; }
7
8int main()
9{
10 std::vector<int> v{0, 3, 6, 9, 12};
11
12 std::cout << std::count_if(v.begin(), v.end(), is_odd) << '\n';
13
14 auto lambda = [](int i){ return i % 3 == 0; };
15 std::cout << std::count_if(v.begin(), v.end(), lambda) << '\n';
16
17 using namespace boost::phoenix::placeholders;
18 auto phoenix = arg1 % 3 == 0;
19 std::cout << std::count_if(v.begin(), v.end(), phoenix) << '\n';
20}
リストv{0, 3, 6, 9, 12}に対して、
14-15行目ではlambdaを使って3で割り切れるものを数えている。
17-19行目ではphoenixを使って3で割り切れるものを数えている。
Phoenixもlambdaも同じことができる。Placeholderの標記の違いなどはある(PhonixのPlaceholderはarg1と書く)が、両者は「ほぼ同じ」と見ている。
$ ./a.out
2
5
5
一応次のような違いがある。。Phoenixの方が後発のライブラリである。
- The phoenix function can accept "ANY" type which the modulo operator can handle while the lambda function expects a parameter type of int.