Store C ++ 0x lambda functions in std :: map / vector for later use in Visual Studio

I am working on a small graphics engine project, and I want it to switch to the platform (someday). I was developing the latest version of MinGW and C ++ 0x. For event listeners, I use lambda functions stored in std :: map, which will be called when a certain event occurs. It works very smoothly with MinGW, but the other day, when I tried it in Visual Studio (latest version), it failed.

I checked the lambda type, and even if I defined two lambdas that were the same, they would get different types (anonymous namespace :: and anonymous namespace: :)).

For example, I have this std :: map for storing scroll listeners

std::map<int,void (*)(int p)> scrollListenerFunctions;

And then I can add a listener by simply doing:

addScrollListener([](int p){/* Do something here */});

As I said, this works fine in MinGW, but doesn’t work in Visual Studio, is there a way to make it work in both cases, and is it even possible to store lambdas in VS atm?

If you need / need to see more code, you can find it here http://code.google.com/p/aotk/source/browse/ lambda maps are located in window.h / window.cpp

+5
source share
2 answers

instead of this:

std::map<int,void (*)(int p)> scrollListenerFunctions;

you should have this:

std::map<int,std::function<void(int p)> > scrollListenerFunctions;

The fact is that lambda does not convert to a pointer function. You need a more general callback wrapper likestd::function

+7
source

lambdas , Visual Studio , , lambdas. , std::function.

+5

All Articles