Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 457
Steve Avatar asked Sep 22 '12 19:09

Steve


People also ask

What does return {} do in C ++?

return {}; indicates "return an object of the function's return type initialized with an empty list-initializer".

Should a function contain a return statement if it does not return a value in C?

A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non- void return type, the compiler issues a warning message.

Can return statement be used with a void function?

A void function can return A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated.

Can auto be a return type in C++?

In C++14, you can just use auto as a return type.


1 Answers

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.

like image 73
JKor Avatar answered Sep 27 '22 21:09

JKor