0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Boost.PhoenixでBoost.Functionライクな関数が定義できるぞ

Last updated at Posted at 2020-08-19

オペレーターのオーバーロードにより、Boost.Phoenixで独自の関数を定義することができる。

 1#include <boost/phoenix/phoenix.hpp>
 2#include <vector>
 3#include <algorithm>
 4#include <iostream>
 5
 6struct is_odd_impl
 7{
 8    typedef bool result_type;
 9
10    template <typename T>
11    bool operator()(T t) const { return t % 3 == 1; }
12};
13
14boost::phoenix::function<is_odd_impl> is_odd;
15
16int main()
17{
18  std::vector<int> v{1, 3, 6, 8, 12};
19
20  using namespace boost::phoenix::placeholders;
21  std::cout << std::count_if(v.begin(), v.end(), is_odd(arg1)) << '\n';
22}

11行目で、operator()をオーバーロードしている。

リストv{1, 3, 6, 8, 12}のうち、3で割って余りが1になる要素の数を数えている(この場合は1だけ)


$ ./a.out 
1

なぜ、このようなことができるようにしているのか?

個人的な感想その1:Boost.Phoenixは後発のライブラリなので、Boost.Functionっぽくも使えるという事をしたかったのではないか?
「'Д・」

個人的な感想その2:オペレーターのオーバーロードこそが、Boost.Function/Lambda/Phoenix/Spiritの礎となる奥義なのではないか?
(*゚ロ゚;)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?