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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:23:45+00:00 2026-05-26T08:23:45+00:00

Possible Duplicate: Recursive lambda functions in c++0x Why can’t I call a lambda recursively

  • 0

Possible Duplicate:
Recursive lambda functions in c++0x

Why can’t I call a lambda recursively if I write it as:

auto a = [&]
{ 
   static int i = 0; i++;
   std::cout << i << std::endl; 
   if (i<10) 
     a(); //recursive call
};

It gives compilation error (ideone):

prog.cpp:8:18: error: '((const main()::<lambda()>*)this)->main()::<lambda()>::a' cannot be used as a function

prog.cpp: In function 'int main()':
prog.cpp:9:9: error: variable 'auto a' with 'auto' type used in its own initializer

What does the error mean?

I understand the reason why I can’t write this:

auto i=i+1; //error: unable to deduce 'auto' from '<expression error>'

We can’t write this because the type of i has to be deduced from it’s initialization, which means the type cannot be deduced if i itself appears in the initialization (ideone). But how does it matter in case of lambda? If I’m not wrong, the type of a lambda is determined by it’s parameter(s) and the return type; it doesn’t depend on the body if it returns nothing (in which case, the return type is deduced as void, irrespective of other statements in the lambda-body).

Anyway, I got a workaround, and I can use std::function instead as:

std::function<void()> a = [&] 
{ 
   static int i = 0; i++;
   std::cout << i << std::endl; 
   if (i<10) 
      a();
};

which compile fines (ideone). But I’m still interested to know the reason why the auto version doesn’t compile.

  • 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-26T08:23:46+00:00Added an answer on May 26, 2026 at 8:23 am

    The reason is that there is no special case for lambda-expression initializers of auto variables.

    Such special cases would be prone to errors and misuses. You need to define the rules when you propose that something like a() should work. How is the operator() looked up? What is the precise state of a‘s type? Will the type be complete? (which implies that you already know the capture list of the lambda). Once you have formulated that in a format reasonable for a spec, it would be easier to make statements on it.

    Allowing your use case would mean yet another case where you need to scan ahead in code, because to determine the type of a in a() you must be sure that the initializer ends with nothing that could “unlambda” the type

    struct y { void operator()() { } };
    template<typename T> y operator+(T, y) { return y(); }
    auto x = [] { x(); } + y();
    

    In this case, x() would call y::operator(), not the lambda.

    As it is now, a is simply forbidden to be mentioned in its entire initializer. Because in C++, auto is not a type. It is merely a type specifier standing for a to-be-deduced type. As a consequence, an expression can never have type auto.

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

Sidebar

Related Questions

Possible Duplicate: Can a recursive function be inline? I think that recursive function defined
Possible Duplicate: Write a recursive function that reverses the input Recently, I've been reading
Possible Duplicate: Design patterns for converting recursive algorithms to iterative ones I can only
Possible Duplicate: recursive array_diff()? I have a static multidimensional array which is always going
Possible Duplicate: else or return? Consider a typical recursive function: public int Fact(n) {
Possible Duplicates: Is there a problem that has only a recursive solution? Can every
Possible Duplicate: What is a RECURSIVE Function in PHP? What is a recursive function
Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
Possible Duplicates: implementing a compiler in “itself” Bootstrapping a language How can you write
Possible Duplicate: How can I take screenshots with Perl? How can I take a

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.