C++ 理解绑定
我在理解std::bind调用时有点困难。 在以下示例中:C++ 理解绑定,c++,c++11,stl,bind,C++,C++11,Stl,Bind,我在理解std::bind调用时有点困难。 在以下示例中: #include <functional> #include <iostream> #include <memory> class Notifier { public: Notifier(std::function<void(Notifier&)> on_notify) :on_notify_(on_notify) { } void notify()
#include <functional>
#include <iostream>
#include <memory>
class Notifier
{
public:
Notifier(std::function<void(Notifier&)> on_notify)
:on_notify_(on_notify)
{ }
void notify()
{
if (on_notify_)
on_notify_(*this);
}
std::function<void(Notifier&)> on_notify_;
};
struct Manager
{
Manager()
{
n_ = std::make_unique<Notifier>(std::bind(&Manager::trigger, this));
}
void trigger()
{
std::cout << "notified" << std::endl;
}
std::unique_ptr<Notifier> n_;
};
int main()
{
Manager s;
s.n_->notify();
}
#包括
#包括
#包括
类通知程序
{
公众:
通知程序(std::notify上的函数)
:on_notify(on_notify)
{ }
作废通知()
{
如果(在通知中)
在通知时通知(*本);
}
std::通知上的函数;
};
结构管理器
{
经理()
{
n_u=std::make_unique(std::bind(&Manager::trigger,this));
}
无效触发器()
{
std::coutstd::bind
基本上会根据忽略无效的给定参数
如果g()调用中提供的某些参数与g中存储的任何占位符都不匹配,则将计算并丢弃未使用的参数
当提供了更荒谬的参数时,绑定的函子仍然可以成功地到达Manager::trigger()
,这可能会让您感到惊讶,如下所示:
#include <functional>
#include <iostream>
#include <memory>
// Some classes that have nothing to do with on_notify_
class AAA {};
class BBB {};
class Notifier
{
public:
Notifier(std::function<void(AAA&, BBB&)> on_notify)
:on_notify_(on_notify)
{ }
void notify()
{
if (on_notify_)
{
// Arguments not matching.
AAA a{};
BBB b{};
// Invoke with them.
on_notify_(a, b);
}
}
std::function<void(AAA&, BBB&)> on_notify_;
};
struct Manager
{
Manager()
{
n_ = std::make_unique<Notifier>(std::bind(&Manager::trigger, this));
}
void trigger()
{
std::cout << "it's also notified!" << std::endl;
}
std::unique_ptr<Notifier> n_;
};
int main()
{
Manager s;
s.n_->notify();
}
#包括
#包括
#包括
//一些与on_无关的类通知_
AAA级{};
类BBB{};
类通知程序
{
公众:
通知程序(std::notify上的函数)
:on_notify(on_notify)
{ }
作废通知()
{
如果(在通知中)
{
//参数不匹配。
AAA{};
BBB{};
//与他们一起调用。
关于通知(a,b);
}
}
std::通知上的函数;
};
结构管理器
{
经理()
{
n_u=std::make_unique(std::bind(&Manager::trigger,this));
}
无效触发器()
{
std::coutstd::bind
基本上会根据忽略无效的给定参数
如果g()调用中提供的某些参数与g中存储的任何占位符都不匹配,则将计算并丢弃未使用的参数
当提供了更荒谬的参数时,绑定的函子仍然可以成功地到达Manager::trigger()
,这可能会让您感到惊讶,如下所示:
#include <functional>
#include <iostream>
#include <memory>
// Some classes that have nothing to do with on_notify_
class AAA {};
class BBB {};
class Notifier
{
public:
Notifier(std::function<void(AAA&, BBB&)> on_notify)
:on_notify_(on_notify)
{ }
void notify()
{
if (on_notify_)
{
// Arguments not matching.
AAA a{};
BBB b{};
// Invoke with them.
on_notify_(a, b);
}
}
std::function<void(AAA&, BBB&)> on_notify_;
};
struct Manager
{
Manager()
{
n_ = std::make_unique<Notifier>(std::bind(&Manager::trigger, this));
}
void trigger()
{
std::cout << "it's also notified!" << std::endl;
}
std::unique_ptr<Notifier> n_;
};
int main()
{
Manager s;
s.n_->notify();
}
#包括
#包括
#包括
//一些与on_无关的类通知_
AAA级{};
类BBB{};
类通知程序
{
公众:
通知程序(std::notify上的函数)
:on_notify(on_notify)
{ }
作废通知()
{
如果(在通知中)
{
//参数不匹配。
AAA{};
BBB{};
//与他们一起调用。
关于通知(a,b);
}
}
std::通知上的函数;
};
结构管理器
{
经理()
{
n_u=std::make_unique(std::bind(&Manager::trigger,this));
}
无效触发器()
{
std::返回类型的属性是否有引用。特别是它的操作符()
。您能再详细说明一下吗?它看起来还是有点像obscure@dau_sama如果调用g()时提供的某些参数与存储在g中的占位符不匹配,则未使用的参数将被计算并丢弃谢谢!我不知道有一个关于返回类型属性的引用。特别是它的操作符()
。你能再详细一点吗?它看起来还是有点obscure@dau_sama“如果调用g()时提供的某些参数如果没有与存储在g中的任何占位符匹配,则未使用的参数将被计算并丢弃。“谢谢!我不知道这一点