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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:05:58+00:00 2026-06-07T11:05:58+00:00

Consider the following code: main() { bool t; … std::function<bool (bool)> f = t

  • 0

Consider the following code:

main()
{
    bool t;
    ...
    std::function<bool (bool)> f = t ? [](bool b) { return b; } : [](bool b) { return !b; }; // OK
    std::function<bool (bool)> f = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
}

When compiled with Clang 3.1, the assignment of non-capture lambda works while the one with captures fails:

main.cpp:12:36: error: incompatible operand types ('<lambda at main.cpp:12:38>' and '<lambda at main.cpp:12:71>')
        std::function<bool (bool)> f2 = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
                                          ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Why does capturing the same variable causes the 2 lambdas to be of incompatible types?

  • 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-07T11:05:59+00:00Added an answer on June 7, 2026 at 11:05 am

    The type of a lambda is “a unique, non-union class type” called the closure type. Each lambda is implemented as a different type, local to the scope of declaration, which has an overloaded operator () to call the function body.

    Example: if you write this:

    auto a=[t](bool b){return t==b;};
    auto b=[t](bool b){return t!=b;};
    

    Then the compiler compiles this (more or less):

    class unique_lambda_name_1 
    {
     bool t; 
    public:
     unique_lambda_name_1(bool t_) t(_t) {}
     bool operator () (bool b) const { return t==b; }
    } a(t); 
    class unique_lambda_name_2
    {
     bool t;
    public: 
     unique_lambda_name_2(bool t_) t(_t) {}
     bool operator () (bool b) const { return t!=b; }
    } b(t); 
    

    a and b have different types and can’t be used in the ?: operator.

    However, §5.1.2(6) says, that the closure type of a lambda with no capture has a non-explicit, public conversion operator, which converts the lambda to a function pointer – non-closures can be implemented as simple functions. Any lambda with the same argument and return types can be converted to the same type of pointer and so the ternary ?: operator can be applied to them.

    Example: the non-capture lambda:

    auto c=[](bool b){return b;};
    

    is implemented like this:

    class unique_lambda_name_3
    {
     static bool body(bool b) { return b; }
     public:
     bool operator () (bool b) const { return body(b); }
     operator decltype(&body) () const { return &body; }
    } c; 
    

    which means that this line:

    auto x = t?[](bool b){return b;}:[](bool b){return !b;};
    

    means actually this:

    // a typedef to make this more readable
    typedef bool (*pfun_t)(bool); 
    pfun_t x = t?((pfun_t)[](bool b){return b;}):(pfun_t)([](bool b){return !b;});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code which shows compile time error : #include <stdio.h> int main(int
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
Consider the following code: int main() { int i; volatile int* p = &i;
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries

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.