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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:22:03+00:00 2026-06-10T23:22:03+00:00

In gcc 4.5, the following code compiles and works as expected with -std=c++0x ,

  • 0

In gcc 4.5, the following code compiles and works as expected with -std=c++0x,

#include <stdio.h>

template<typename H>
void caller(H h)
{
    h();
}

int main()
{
    auto c = [](){ printf("A\n"); };
    caller(c);
    caller([](){ printf("B\n"); });
    return 0;
}

Prints,

A
B

However, if caller is defined to take a reference,

template<typename H>
void caller(H &h)
{
    h();
}

The compiler complains,

test.cpp: In function ‘int main()’:
test.cpp:61:34: error: no matching function for call to ‘caller(main()::<lambda()>)’
test.cpp:52:6: note: candidate is: void caller(H&) [with H = main()::<lambda()>]

Why?

This seems to break the idea of lambdas providing value semantics for functions, but moreover it means I can’t write certain small functions inline which is a bit annoying.

(Is this fixed in newer versions of gcc? I haven’t had a chance to test.)

Edit: I just discovered the following actually works:

template<typename H>
void caller(H *h)
{
    (*h)();
}

int main()
{
    auto c = [](){ printf("A\n"); };
    caller(&c);
    caller(&([](){ printf("B\n"); }));
}

I didn’t think I’d be able to take the address of a temporary like that. Maybe that sort of solves the problem, though annoying to require users of the function to pass in the address of the closure instead of a convenient reference.

  • 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-10T23:22:04+00:00Added an answer on June 10, 2026 at 11:22 pm

    You’re trying to pass a temporary by non-const reference. That won’t work for any type.

    Pass the lambda by const reference instead.

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

Sidebar

Related Questions

I have the following code: #include <stdio.h> #include <math.h> int main(void) { printf(%f\n, fmax(1.2,
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I have the following code compiled by gcc: #include <iostream> using namespace std; class
The following code works. /* hello.cc */ #include <iostream> #include <vector> void vec_print() {
The following example code compiles under gcc and works as I would hope. It
The following code: #include <stdio.h> typedef union { int n; char *s; } val_t;
I am trying to compile the following program: #include <iostream> int main(){ std::cout <<
I was playing with variadic template parameters using gcc 4.6.1. The following code compiles
The following code compiles with gcc-4.5.1 but not in Visual Studio 11. #include <map>
The following code gives a compiler error (gcc-4.7 run with -std=c++11 ): #include <iostream>

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.