C++ 对于带有成员函数指针和参数的每个调用函数

C++ 对于带有成员函数指针和参数的每个调用函数,c++,lambda,member-function-pointers,C++,Lambda,Member Function Pointers,我做了一个模板列表的简单实现: template<typename T>List{ [...] private: class ListElement{ ListElement * next; T* value; }; ListElement *first, *last; }; 每次我想对每个存储值调用一个方法时,我都会用如下方式调用该函数: void call_update(Item* item){ item-

我做了一个模板列表的简单实现:

template<typename T>List{
    [...]
private:
    class ListElement{
        ListElement * next;
        T* value;
    };
    ListElement *first, *last;
};
每次我想对每个存储值调用一个方法时,我都会用如下方式调用该函数:

void call_update(Item* item){
    item->update(globally_set_update_value_before_calling_for_each);
}
但我已经创建了8种不同的全局定义的“call_X”方法,这开始让人恼火

我可以实现我上面描述的吗?Lambda表达式在这里也可以很好地使用

是的,我正试图明确地解决任何std::stuff问题,并看看在没有它的情况下如何自己实现它。

您对“也可以将任意参数传递给这些函数调用”的要求几乎需要使用模板和参数包

template<typename func_type, typename ...Args>
void for_each_call(func_type &&func, Args && ...args)
{
    for(ListElement * current = first; current != nullptr; current = current->next)
         func(current->value, std::forward<Args>(args)...);
}
模板
每个调用(func类型和func、Args和…Args)无效
{
对于(ListElement*current=first;current!=nullptr;current=current->next)
func(当前->值,标准::正向(参数)…);
}

您不应该需要参数。。。如果你传递一个lambda。你不需要,但这也应该与一个普通的函数指针一起工作。另外,解决方案使用std::forward,这是不好的,因为我不想使用std::---这是我自己做的练习的重点。我该如何调用它?对于每个调用(lambda/class/functionptr[,任何额外参数>])*)
void call_update(Item* item){
    item->update(globally_set_update_value_before_calling_for_each);
}
template<typename func_type, typename ...Args>
void for_each_call(func_type &&func, Args && ...args)
{
    for(ListElement * current = first; current != nullptr; current = current->next)
         func(current->value, std::forward<Args>(args)...);
}