如何使用 std::bind with lambda
How to use std::bind with lambda
我正在尝试在我的lambda中使用std::bind :
#include <functional>
#include <iostream>
#include <string>
struct Foo {
Foo() {}
void func(std::string input)
{
std::cout << input << 'n';
}
void bind()
{
std::cout << "bind attempt" << 'n';
auto f_sayHello = [this](std::string input) {std::bind(&Foo::func, this, input);};
f_sayHello("say hello");
}
};
int main()
{
Foo foo;
foo.bind();
}
当我运行此代码时,我所期望的是看到以下输出
bind attempt
say hello
但我只看到"绑定尝试"。 我很确定 lambda 有一些我不明白的地方。
std::bind(&Foo::func, this, input);
这将调用 std::bind
,这将创建一个调用 this->func(input);
的函子。但是,std::bind
本身并不调用this->func(input);
。
你可以使用
auto f = std::bind(&Foo::func, this, input);
f();
或
std::bind(&Foo::func, this, input)();
但是在这种情况下,为什么不以简单的方式编写呢?
this->func(input);
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 可组合的lambda/std::函数与std::可选
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- Problems with std::cin.fail()
- Pybind11: init<> with lambda
- C++, Fulfilling typedef with lambda?
- 在 lambda(parameter) 中为 std::transform with std::sregex_token
- std::function std::bind with lambda overload ambiguity
- std::max with lambda and auto
- std::make_unique<std::thread> with lambda
- Using boost::lambda with boost::thread
- QtConcurrent::run with lambda on mac os 没有提供匹配函数
- 如何使用 std::bind with lambda
- creating unordered_set with lambda
- ptr_fun with a lambda function
- c++ lambda with捕获作为函数指针
- EXC_BAD_ACCESS with lambda capture
- Std::transform with lambda:跳过一些项
- Max_element with lambda:如何编译