Skip to content

Commit

Permalink
feat: std::bindでメンバー関数を呼び出す方法を例に追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yumetodo committed Feb 2, 2023
1 parent a689c6b commit 4136995
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions reference/functional/bind.md
Expand Up @@ -66,16 +66,34 @@ int add(int a, int b, int c)
return a + b + c;
}

class Foo {
public:
int n;
Foo(int n) : n(n) {}
Foo add(int n2) const
{
return this->n + n2;
}
};

int main()
{
// 第1引数のみを先に渡す
using namespace std::placeholders;
std::function<int(int, int)> f = std::bind(add, 2, _1, _2);
std::function<int(int, int)> f1 = std::bind(add, 2, _1, _2);

// 残りの引数を渡して関数を呼び出す
const int result1 = f1(3, 4);

Foo foo{2};

// thisにするもののみを先に渡す
std::function<Foo(int)> f2 = std::bind(&Foo::add, foo, _1);

// 残りの引数を渡して関数を呼び出す
const int result = f(3, 4);
const auto result2 = f2(3);

std::cout << result << std::endl;
std::cout << result1 << ',' << result2.n << std::endl;
}
```
* std::bind[color ff0000]
Expand All @@ -84,7 +102,7 @@ int main()
### 出力
```
9
9,5
```
## バージョン
Expand Down

0 comments on commit 4136995

Please sign in to comment.