Setting Callbacks with Lambdas

While I was working on a project that used glfw, I started to get increasingly frustrated with declaring functions, only to use them as a callback in one place. Sure, they can be declared in an anonymous namespace so that they don’t cause any sort of namespace collisions, but it still looks bothersome and just feels wrong. So, taking advantage of the lambda expressions introduced in C++ 11, I took a long shot and tried to set a callback using a lambda. And it worked, it compiled without fail!

The Code

I’m sure this is a no-brainer to most people, and granted it isn’t terribly complicated by any means.
But I don’t think I’ve seen an example of this anywhere before.
So, if someone sees this and can write code that’s a bit more clean, this post has done it’s job.

Leave a Reply

Your email address will not be published. Required fields are marked *