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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:35:28+00:00 2026-05-26T18:35:28+00:00

Look at this template: template <class T> auto checkErrorCode(T& functor) -> decltype(functor()) { auto

  • 0

Look at this template:

template <class T>
auto checkErrorCode(T& functor) -> decltype(functor()) {
    auto retCode = functor();
    // ... additional aspect-like stuff with retCode here
    return retCode;
}

It is supposed to execute a lambda passed inside it,
get the return value from the lambda, do something with it,
and then return it back to the caller. Think “aspect programming”,
logging, whatever.

Even though it compiles and works fine with this…

int foo(int param)
{
    return param>10?0:-1;
}

main()
{
    int param = 11;

    // This compiles fine
    auto action = [&](){ return foo(param); };
    checkErrorCode( action );
}

…it doesn’t compile – and emits a hard to understand error – when I directly call “checkErrorCode” with an inline lambda:

int foo(int param)
{
    return param>10?0:-1;
}

main()
{
    int param = 11;

    // This doesn't compile...
    checkErrorCode( [&](){ return foo(param); } );
}

Any thoughts as to why?

g++ emits this weird error:

forSO.cpp: In function 'int main()':
forSO.cpp:24:5: error: no matching function for call to 'checkErrorCode(main()::<lambda()>)'
forSO.cpp:24:5: note: candidate is:
forSO.cpp:2:6: note: decltype (functor()) checkErrorCode(T&) [with T = main()::<lambda()>, decltype (functor()) = int]
forSO.cpp:2:6: note:   no known conversion for argument 1 from 'main()::<lambda()>' to 'main()::<lambda()>&'

In the unlikely case that this is a compiler bug:

bash$ g++ -v
...
gcc version 4.6.2 (GCC) 
  • 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-26T18:35:28+00:00Added an answer on May 26, 2026 at 6:35 pm

    The template needs to use an rvalue reference:

    template <class T>
    auto checkErrorCode(T&& functor) -> decltype(functor()) {
        auto retCode = functor();
        // ... additional aspect-like stuff with retCode here
        return retCode;
    }
    

    Or, const lvalue (const T&). Because lambdas are temporary (rvalue) functions, you can only pass them by const (lvalue) reference or by rvalue reference.

    As a rule, keep lvalue refs const unless you plan to do non-const accesses (e.g. left side of assignment, call of non-const member function, etc.)
    If you’re doing continuations, it can get a bit more complicated because then you have to pass a non-const lvalue reference. But you’d have an explicit functor class, so non-const lvalue ref creation is allowed. In this case, it’s clearer to use a rvalue ref for lambdas, as const lvalue ref is the C++98 way of doing things, and limitations of that system were a motivating factor for creating rvalue references.

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

Sidebar

Related Questions

Please take a look at this code: template<class T> class A { class base
Given a template like template<int dim> class Point { ... }; this template can
Does this look like it should work? I'm wanting to generate directions from one
I am writing matrix classes. Take a look at this definition: template <typename T,
Most of my classes have debug variables, and this makes them often look like
I have 2 models which look like that: class Entry(models.Model): user = models.ForeignKey(User) dataname
I suppose to do some unusual stuff. I have a class template: template<class T>
I've got a compile-time recursive type that looks somewhat like this: template <typename DataType,
difficult to look this up if it has been asked previously since I don't
Not exactly sure how to look this up, but I'm not finding the solution

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.