I currently have the following code:
using boost::bind;
ty开发者_如何学运维pedef boost::signal<void(EventDataItem&)> EventDataItemSignal;
class EventDataItem
{
...
EventDataItemSignal OnTrigger;
...
}
typedef std::list< shared_ptr<EventDataItem> > DataItemList;
typedef std::list<boost::signals::connection> ConnectionList;
class MyClass
{
void OnStart()
{
DataItemList dilItems;
ConnectionList clConns;
DataItemList::iterator iterDataItems;
for(iterDataItems = dilItems.begin();
iterDataItems != dilItems.end();
iterDataItems++)
{
// Create Connections from Triggers
clConns.push_back((*iterDataItems)->OnTrigger.connect(
bind(&MyClass::OnEventTrigger, this)));
}
}
void OnEventTrigger()
{
// ... Do stuff on Trigger...
}
}
I'd like to change MyClass::OnStart to use std::transform to achieve the same thing:
void MyClass::OnStart()
{
DataItemList dilItems;
ConnectionList clConns;
// Resize connection list to match number of data items
clConns.resize(dilItems.size());
// Build connection list from Items
// note: errors on the placeholder _1->OnTrigger
std::transform(dilItems.begin(), dilItems.end(),
clConns.begin(),
bind(&EventDataItemSignal::connect, _1->OnTrigger,
bind(&MyClass::OnEventTrigger, this)));
}
However, my hiccup is _1->OnTrigger. How can I reference OnTrigger from placeholder _1?
You can solve it in the same way as Accessing member variables through boost lambda placeholder: replace _1->OnTrigger
with bind(&EventDataItem::OnTrigger, _1)
.
精彩评论