Home:ALL Converter>Any way to match on return value in C++11?

Any way to match on return value in C++11?

Ask Time:2012-09-23T03:35:24         Author:Steve

Json Formatter

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!

Author:Steve,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12547076/any-way-to-match-on-return-value-in-c11
JKor :

You could use std::enable_if.\n\nYour 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.",
2012-09-22T19:45:57
yy