一千萬個為什麽

搜索

如何獲取占位符:: _ 1的內容

我目前有以下代碼:

using boost::bind;

typedef boost::signal EventDataItemSignal;
class EventDataItem
{
...
EventDataItemSignal OnTrigger;
...
}

typedef std::list< shared_ptr > DataItemList;
typedef std::list  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...
}
}

我想改變MyClass :: OnStart使用std :: transform來實現同樣的目的:

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).

轉載註明原文: 如何獲取占位符:: _ 1的內容