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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:08:40+00:00 2026-06-12T04:08:40+00:00

I am trying to provide a function which calls a provided lambda, and I’m

  • 0

I am trying to provide a function which calls a provided lambda, and I’m wondering if it’s possible to have it return a default value if the function has return type void.

Here’s what I have so far, a function which returns the lambda’s return value, but if the lambda is void, then it returns 20.

#include <functional>
#include <iostream>

template <typename H>
auto f(H&& h) -> decltype(h(), void())
{
    return h();
}

template <typename H>
auto f(H&& h) -> decltype(h(), int())
{
    h();
    return 20;
}

int main()
{
    int r = f([](){ std::cout << "test1" << std::endl; return 10; }); // error here
    std::cout << "r: " << r << std::endl;
    r = f([](){ std::cout << "test2" << std::endl; });
    std::cout << "r: " << r << std::endl;

    return 0;
}

This produces the error,

test.cpp:20:68: error: call of overloaded ‘f(main()::<lambda()>)’ is ambiguous

So clearly this is just due to C++ not being able to use polymorphism based on return type. However, I am wondering if there are any good C++11 tricks, such as better use of decltype or some template magic, that might make this possible? I am asking because as far as I can see there is no real ambiguity here.. the compiler is inferring the void return type and then saying that it’s ambiguous whether to match the int or void version of f, which is a bit silly.

A reason for doing this is that if the function f expects a return value but the user provides a lambda that doesn’t include a return statement, the compiler infers a void type, and errors out. Since in the real scenario I have a good idea of what a reasonable default return value should be when the user doesn’t care to provide one, I am wondering if it’s possible to have the compiler allow users to neglect the often-unnecessary return statement for convenience.

Thanks. I should mention that I am using GCC 4.6.3.

Answer: based on Xeo’s suggestion of using enable_if, I came up with the following, which seems to work:

template <typename H>
auto f(H&& h) -> typename std::enable_if<std::is_same<decltype(h()), void>::value, int>::type
{
    h();
    return 20;
}

template <typename H>
auto f(H&& h) -> typename std::enable_if<std::is_same<decltype(h()), int>::value, int>::type
{
    return h();
}

Thanks!

  • 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-12T04:08:41+00:00Added an answer on June 12, 2026 at 4:08 am

    You could use std::enable_if.

    Your return type would be std::enable_if<!std::is_same<decltype(h()), void>::value, decltype(h())>:type for the first one, and std::enable_if<std::is_same<decltype(h()), void>::value, int>::type for the second one. That should get your code working. I haven’t tested it, so I’m not sure if it works fully.

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

Sidebar

Related Questions

trying to refactor code to provide clean association A GAME has a HOME_TEAM and
I have a buggy kernel module which I am trying to fix. Basically when
I have a javascript function that I am trying to add to my document,
I have this static function which basicly makes a connection to a webpage, send
I am trying to make a WCF call to a function which accepts one
I'm trying to overload a Sum function which accepts a [list or vector] start
I have an ajax function which call a servlet to get list of products
I'm trying to provide a link to the attachment of a note through the
I am trying to provide a simple RESTful API to my ASP MVC project.
I am trying to provide an interface to an abstract generic base class. I

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.