如何从单个C ++ return语句返回多个值中的一个? [英] How to return one out of multiple values from a single C++ return statement?

查看:111
本文介绍了如何从单个C ++ return语句返回多个值中的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

return a or b or c or d;

该语句在C ++中返回 true false ,我知道原因.

That statement returns either true or false in C++, and I know the reason for this.

但是我需要一种解决方法,以便可以通过该return语句(或类似的东西)返回第一个非零 ,就像在Python中一样.

But I need a workaround so that I can return the first non-zero value via that return statement (or something similar) like it happens in Python.

我不是在寻找条件语句,因为它看起来很不整洁有时.

I am not looking for conditional statements as it looks untidy sometimes.

基本上,下面的代码可以通过宏或其他方式缩短吗?

Basically, can the following code be shortened via a macro or something else?

int fun(int a, int b, int c, int d)
{
    return a ? a : b ? b : c ? c : d;
}

推荐答案

我会这样写函数:

int fun(int a, int b, int c, int d) {
    if (a) return a;
    else if (b) return b;
    else if (c) return c;
    return d;
}

干净整洁.我可以在这里停下来,但是让我们探索可以做什么...

It is clean and short. I could stop here, but lets explore what can be done...

有一种算法已经可以满足您的要求.对此答案中的解决方案进行了轻微修改:

There exists an algorithm that already almost does what you want. A slight modification of the solution in this answer:

#include <algorithm>
#include <initializer_list>

template <typename T>
T first_non_zero_or_zero(const std::initializer_list<T>& args)
{
    auto it = std::find_if_not(args.begin(),args.end(),[](auto v){ return v==0;});    
    return (it != args.end()) ? *it : 0;
}

对布尔表达式使用函数的缺点是不会使人心烦意乱.如果您通过以下方式调用该函数:

The drawback of using a function for boolean expressions is no short-cuirciting. If you call the function via:

auto x = first_non_zero_or_zero( { foo(), expensive_function() });

然后,无论 foo 返回什么,都必须调用 expensive_function .恢复短路能力的方法是传递可调用对象,这将是

Then expensive_function must be called, no matter what foo returns. The way to restore the ability to short-circuit is to pass callables instead, that would be

template <typename F>
auto first_non_zero_or_zero(F f){ return f();}

template <typename First,typename...F>
auto first_non_zero_or_zero(First f,F... others){    
    if (auto temp = f()) return temp;
    return first_non_zero_or_zero(others...);
}

int foo(){ return 0;}
int expensive_function(){ return 42;}

int main()
{
    std::cout << first_non_zero_or_zero(foo,expensive_function);
    return 0;
}

但是,当使用简单的 int s进行调用时,这会使调用变得不必要的冗长,因为您需要将它们包装在可调用的容器中:

However, this will make calls unnecessarily verbose when called with simple ints, as you need to wrap them in a callable:

int fun(int a,int b,int c) {
    first_non_zero( [](){ return a;},
                    [](){ return b;},
                    [](){ return c;})
}

结论:不要使事情变得不必要的复杂.函数应该做一件事.您的 fun 要做的一件事是返回4个整数的第一个非零值,而 if-else 是完成此操作的最简单方法.

Conclusion: Don't make things more complicated than necessary. Functions should do one thing. The one thing your fun does is to return the first non-zero of 4 integers and a if-else is the most simple way to get that done.

这篇关于如何从单个C ++ return语句返回多个值中的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆