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

  • SEARCH
  • Home
  • 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 8882173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:30:39+00:00 2026-06-14T20:30:39+00:00

Basically, what I want to be able to do is take a lambda with

  • 0

Basically, what I want to be able to do is take a lambda with any number of any type of parameters and convert it to an std::function.
I’ve tried the following and neither method works.

std::function([](){});//Complains that std::function is missing template parameters
template <typename T> void foo(function<T> f){}
foo([](){});//Complains that it cannot find a matching candidate

The following code does work however, but it is not what I want because it requires explicitly stating the template parameters which does not work for generic code.

std::function<void()>([](){});

I’ve been mucking around with functions and templates all evening and I just can’t figure this out, so any help would be much appreciated.

As mentioned in a comment, the reason I’m trying to do this is because I’m trying to implement currying in C++ using variadic templates. Unfortunately, this fails horribly when using lambdas. For example, I can pass a standard function using a function pointer.

template <typename R, typename...A>
void foo(R (*f)(A...)) {}
void bar() {}
int main() {
    foo(bar);
}

However, I can’t figure out how to pass a lambda to such a variadic function. Why I’m interested in converting a generic lambda into an std::function is because I can do the following, but it ends up requiring that I explicitly state the template parameters to std::function which is what I am trying to avoid.

template <typename R, typename...A>
void foo(std::function<R(A...)>) {}
int main() {
    foo(std::function<void()>([](){}));
}
  • 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-06-14T20:30:40+00:00Added an answer on June 14, 2026 at 8:30 pm

    You can’t pass a lambda function object as an argument of type std::function<T> without explicitly specifying the template argument T. Template type deduction tries to match the type of your lambda function to the std::function<T> which it just can’t do in this case – these types are not the same. Template type deduction doesn’t consider conversions between types.

    It is possible if you can give it some other way to deduce the type. You can do this by wrapping the function argument in an identity type so that it doesn’t fail on trying to match the lambda to std::function (because dependent types are just ignored by type deduction) and giving some other arguments.

    template <typename T>
    struct identity
    {
      typedef T type;
    };
    
    template <typename... T>
    void func(typename identity<std::function<void(T...)>>::type f, T... values) {
      f(values...);
    }
    
    int main() {
      func([](int x, int y, int z) { std::cout << (x*y*z) << std::endl; }, 3, 6, 8);
      return 0;
    }
    

    This is obviously not useful in your situation though because you don’t want to pass the values until later.

    Since you don’t want to specify the template parameters, nor do you want to pass other arguments from which the template parameters can be deduced, the compiler won’t be able to deduce the type of your std::function argument.

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

Sidebar

Related Questions

Basically I want to take the following: And make it match the styling of
Given this structure.. I basically want to be able to take a list of
Basically I want to be able to have each node of type tree have
I basically want to be able to deploy multiple versions of the same EAR
Basically I want to write a script to be able to detect when the
I have a scraping object basically. I want to be able to add POST
I have two action methods that are conflicting. Basically, I want to be able
Basically I want to be able to invoke a given command, in this case
So I have this code. Basically it should be able to take a stock
I want to know if any ErrorProvider are active in my form. being able

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.