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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:48:08+00:00 2026-05-28T05:48:08+00:00

I’ve written some code using lambda on vc2010. The simplified structure of code is

  • 0

I’ve written some code using lambda on vc2010. The simplified structure of code is like below:

template<typename Functor>
bool f1(int a, Functor& f)
{
    return f(a+1);
}

template<typename Functor>
bool f2(Functor& f)
{
    return f(1);
}

template<typename Functor>
bool f3(Functor& f)
{
    return f2([&](int a) -> bool {
        // (1) Works
        auto test = [&](int b) -> bool { return f(a+b); };
        return f1(a, test);

        // (2) Doesn't work         
        //return f1(a, [&](int b) -> bool { return f(a+b); });
    });
};

int main()
{
    int a = 100;

    f3([&](int b) { return (a+b)%2 == 0; });
}

First I wrote a nested lambda code as (2), and vc10 gives a incomprehensible error message like below

'f1' : cannot convert parameter number from 'int' to 'int &'

However code (1) works well, which is indentical to (2) except lvalue-ness.

My question is:

  1. Is code (2) conforming C++11 standard?
  2. If not, what’s the reason of this strange compilation behavior?
  • 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-28T05:48:08+00:00Added an answer on May 28, 2026 at 5:48 am
    return f1(a, [&](int b) -> bool { return f(a+b); });
    

    Here, the second argument which is a lambda, is a temporary object which cannot be bound to non-const reference. I believe, the error message is misleading; it doesn’t exactly tell the problem. A good compiler will print better error message. Try GCC, or Clang.

    The fix is this: make the second argument non-reference:

    template<typename Functor>
    bool f1(int a, Functor f)  //I removed `&` from the second parameter
    {
        return f(a+1);
    }
    

    Likewise, make all the Functor parameters in other functions non-reference. Passing functor as reference doesn’t make much sense, especially in C++11, in which you can pass lambda as well which you can define on the fly.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.