English 中文(简体)
Invoke 困:在 st中运行:for_each
原标题:Invoke std::function in an std::for_each
  • 时间:2012-04-20 17:06:40
  •  标签:
  • c++
  • c++11
  • std

我基本上有以下法典:

typedef std::function<void ()> fnGlobalChangeEvent;
typedef std::vector<fnGlobalChangeEvent> GlobalTriggers;

inline void ExecuteGlobal(fnGlobalChangeEvent ev)
{
    ev();
}

GlobalTriggers triggers;
std::for_each(triggers.begin(), triggers.end(), std::bind(&ExecuteGlobal, _1));

在这里,使用赤贫全球感觉是完全多余的,但我找不到正确的辛迪加来放弃这一呼吁。

std::for_each(triggers.begin(), triggers.end(), ExecuteGlobal(_1));
std::for_each(triggers.begin(), triggers.end(), std::bind(_1));

两者都没有汇编。

还有一个更为复杂的案例:

typedef std::function<void (Zot&)> fnChangeEvent;
typedef std::vector<fnChangeEvent> Triggers;

inline void Execute(fnChangeEvent ev, Zot& zot)
{
    ev(zot);
}

Triggers triggers;
std::for_each(triggers.begin(), triggers.end(), std::bind(&Execute, _1, zot));

如果没有帮助者在这些情况下发挥职能是可能的?

最佳回答

Sure, a lambda:

std::for_each(
    triggers.begin(), triggers.end(),
    [](fnChangeEvent ev) { ev(); }
);
std::for_each(
     triggers.begin(), triggers.end(),
     [&zot](fnChangeEvent ev) { ev(zot); }
);

或者说,范围越好:

for (auto ev : triggers) {
    ev();
}

// well, I think you can figure out the second one
问题回答

为什么不把lam子用作:

std::for_each(triggers.begin(), 
              triggers.end(), 
              [&](fnChangeEvent & e) 
              {
                   e(zot);
              });

a. 或使用范围以下的 lo:

for (auto& e : triggers)  { e(zot); }

更简明扼要。

在这里,我刚才所想的话告诉我,这是否像你所想的:

template<typename IT, typename ...Args>
void call_each(IT begin_, IT end_, Args&&... args)
{
    for (auto i = begin_; i!=end_; ++i)
        (*i)(std::forward<Args>(args)...);
}

然后,你可以这样使用:

call_each(triggers.begin(), triggers.end());

对于具有论点的职能:

call_each(triggers.begin(), triggers.end(), zot);

同样,我也 forget忘记了这一选择:

typedef std::function<void ()> fnGlobalChangeEvent;
typedef std::vector<fnGlobalChangeEvent> GlobalTriggers;

GlobalTriggers triggers;
using namespace std::placeholders;
std::for_each(triggers.begin(), triggers.end(), std::bind(&fnGlobalChangeEvent::operator(), _1));




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签