Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:28:14+00:00 2026-05-16T01:28:14+00:00

I am trying to use a lambda to pass in place of a function

  • 0

I am trying to use a lambda to pass in place of a function pointer but VS2010 can’t seem to convert it. I have tried using std::function like this and it crashes and I have no idea if I am doing this right!

#include <windows.h>
#include <conio.h>

#include <functional>
#include <iostream>

#include <concrt.h>


void main()
{
    std::function<void(void*)> f = [](void*) -> void
    {
        std::cout << "Hello\n";
    };


    Concurrency::CurrentScheduler::ScheduleTask(f.target<void(void*)>(), 0);

    getch();
}

It seems strange to me that the compiler can’t convert such a lambda to a simple function pointer as it captures no variables – also in the case that it did I wonder what can be done.

Is the type of each lambda unique? So I could hack around with a template function using the lambdas’ type as a template argument to generate a unique static function that could be called instead and hopefully optimised out?

UPDATED

The below seems to work but is it safe?

#include <windows.h>
#include <conio.h>

#include <iostream>

#include <concrt.h>


template<typename Signature>
struct Bind
{
    static Signature method;

    static void Call(void* parameter)
    {
        method(parameter);
    }
};


template<typename Signature>
Signature Bind<Signature>::method;


template<typename Signature>
void ScheduleTask(Signature method)
{
    Bind<Signature>::method = method;
    Concurrency::CurrentScheduler::ScheduleTask(&Bind<Signature>::Call,0);
}


void main()
{
    ScheduleTask
    (   
        [](void*)
        {
            std::cout << "Hello";
        }
    );


    ScheduleTask
    (   
        [](void*)
        {
            std::cout << " there!\n";
        }
    );


    getch();
}

UPDATED AGAIN

So with the help given I have come up with the shorter:

template<typename Signature>
void (*LambdaBind(Signature))(void*)
{
    struct Detail
    {
        static void Bind(void* parameter)
        {
            Signature method;

            method(parameter);
        }
    };


    return &Detail::Bind;
}

This can be used to wrap a lambda with no closure of void(*)(void*) into the equivalent function pointer. It appears that this will become unnecessary in a later version of VS2010.

So how to get this to work for a lambda with closures?

UPDATED AGAIN!

Works for closures in VS2010 – no idea if it’s ‘safe’ though…

template<typename Signature>
struct Detail2
{
    static std::function<void(void*)> method;


    static void Bind(void* parameter)
    {
        method(parameter);
    }
};


template<typename Signature>
std::function<void(void*)> Detail2<Signature>::method;


template<typename Signature>
void (*LambdaBind2(Signature method))(void*)
{
    Detail2<Signature>::method = method;
    return &Detail2<Signature>::Bind;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:28:15+00:00Added an answer on May 16, 2026 at 1:28 am

    This feature of lambda’s was added after VS2010 implemented them, so they don’t exist in it yet.

    Here’s a possible generic work-around, very untested:

    #include <functional>
    #include <iostream>
    
    namespace detail
    {
        // helper specializations,
        // define forwarding methods
        template <typename Lambda, typename Func>
        struct lambda_wrapper;
    
        #define DEFINE_OPERATOR \
                typedef decltype(&call) function_type; \
                operator function_type(void) const \
                { \
                    return &call; \
                }
    
        template <typename Lambda, typename C, typename R>
        struct lambda_wrapper<Lambda, R (C::*)(void) const>
        {
            static R call(void)
            {
                Lambda x;
                return x();
            }
    
            DEFINE_OPERATOR
        };
    
        template <typename Lambda, typename C, typename R,
                    typename A0>
        struct lambda_wrapper<Lambda, R (C::*)(A0) const>
        {
            static R call(A0&& p0)
            {
                Lambda x;
                return x(std::forward<A0>(p0));
            }
    
            DEFINE_OPERATOR
        };
    
        // and so on
        #undef DEFINE_OPERATOR
    }
    
    // wraps a lambda and provides 
    // a way to call it statically
    template <typename Lambda>
    struct lambda_wrapper :
            detail::lambda_wrapper<Lambda, decltype(&Lambda::operator())>
    {};
    
    template <typename Lambda>
    lambda_wrapper<Lambda> wrap_lambda(const Lambda&)
    {
        return lambda_wrapper<Lambda>();
    }
    
    int main(void)
    {
        auto l = [](){ std::cout << "im broked :(" << std::endl; };
        std::function<void(void)> f = wrap_lambda(l);
    
        f();
    }
    

    Let me know if any part is confusing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Lambda Expressions in a project to map to a
I'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
Trying to use a guid as a resource id in a rest url but
While trying to use LINQ to SQL I encountered several problems. I have table
I'm trying to use lambda syntax to be able to swap out search rule
I'm using IronRuby and trying to come up with a seamless to pass a
Trying to use an excpetion class which could provide location reference for XML parsing,
I' trying to use a Linq query to find and set the selected value
I trying to use ImageInfo and Jython to get information from a image on
I'm trying to use svnmerge.py to merge some files. Under the hood it uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.