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 3982616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:32:56+00:00 2026-05-20T05:32:56+00:00

I want get the type of lambda as the template argument. How to do

  • 0

I want get the type of lambda as the template argument. How to do this?

template<class T>
class Foo
{
public:
    Foo(T t){ t(); }
};

int main()
{   
    Foo< type?? > foo([](){ cout<<"construct OK"; });

    system("pause");
    return 0;
}
  • 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-20T05:32:57+00:00Added an answer on May 20, 2026 at 5:32 am

    It’s possible to deduce the type of a lambda-expression when it is the argument to a function template function parameter whose type is deduced.

    Lambda expressions however are explicitly forbidden to appear inside an unevaluated operand — this includes decltype. It was found that this includes several yet unhandled special cases and it was found that there is no real utility to allow lambda expressions inside decltype and friends. Recall that every lambda expression creates a new unique type.

    You can instead use std::function<void()>, which is able to store any function object that can be called without arguments and yields void as return type.

    Foo< std::function<void()> > foo([](){ cout<<"construct OK"; });
    

    Of course, as I said above, function templates can deduce their argument type, so you can also make the constructor a template

    class Foo
    {
    public:
        template<typename T>
        Foo(T t){ t(); }
    };
    
    Foo foo([](){ cout<<"construct OK"; });
    

    But I take it that this is not really useful in your case, since you presumably want to do something with T inside your class definition. Like, storing the object as a non-static data member. With std::function, that’s possible.

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

Sidebar

Related Questions

I want to convert from public class Party { public string Type { get;
I want to get a type of a BasePage object that I am creating.
I want to get the MIME type from a filename using C. Is there
I want to do this: MethodInfo m = myList.GetType().GetMethod(ConvertAll, System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object)); List<object> myConvertedList = (List<object>)m.Invoke(myList,
i have class, with fields of double? type. with reflection i get fields Parameters
Possible Duplicate: Get generic type of java.util.List I have a Map and I want
I want to get started doing some game development using Microsoft's XNA. Part of
I want to get the MD5 Hash of a string value in SQL Server
I want to get my databases under version control. I'll always want to have
I want to get an overview of files that are updated in TFS (that

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.