从c++函数中返回多个值的首选方法是什么?例如,假设有一个函数对两个整数进行除法并同时返回商和余数。我经常看到的一种方法是使用引用形参:

void divide(int dividend, int divisor, int& quotient, int& remainder);

一种变体是返回一个值,并通过引用形参传递另一个值:

int divide(int dividend, int divisor, int& remainder);

另一种方法是声明一个结构体来包含所有的结果并返回:

struct divide_result {
    int quotient;
    int remainder;
};

divide_result divide(int dividend, int divisor);

这些方法中是否有一种通常是首选的,还是有其他的建议?

编辑:在实际代码中,可能会有两个以上的结果。它们也可能是不同类型的。


当前回答

我们可以这样声明函数,它返回一个用户定义的结构类型变量或指向它的指针。通过结构的属性,我们知道C语言中的结构可以保存多个不对称类型的值(即一个int变量,四个char变量,两个float变量等等)。

其他回答

快速回答:

#include <iostream>
using namespace std;

// different values of [operate] can return different number.
int yourFunction(int a, int b, int operate)
{
    a = 1;
    b = 2;

    if (operate== 1)
    {
        return a;
    }
    else
    {
        return b;
    }
}

int main()
{
    int a, b;

    a = yourFunction(a, b, 1); // get return 1
    b = yourFunction(a, b, 2); // get return 2

    return 0;
}

如果你通过引用函数返回一个值,编译器不能存储在寄存器当调用其他函数,因为从理论上讲,第一个函数可以保存变量的地址传递给它的全局变量,和任何subsecuently称为功能可能改变它,所以编译器会有(1)将值从寄存器保存到内存之前调用其他函数和(2)重读的时候需要从内存后再任何这样的调用。

如果通过引用返回,程序的优化将受到影响

这里是关于这个主题的“核心指南”(由Bjarne Stroustrup和Herb Sutter撰写)的链接。

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-out-multi

部分报价:

F.21:要返回多个“out”值,最好返回一个结构体或元组 原因返回值是作为“仅输出”值自记录的。注意,根据使用元组(包括pair)的惯例,c++确实有多个返回值,可能在调用点使用额外方便的tie或结构化绑定(c++ 17)。首选使用有返回值语义的命名结构。否则,无名元组在泛型代码中很有用。

对于返回两个值,我使用std::pair(通常是类型定义的)。您应该查看boost::tuple(在c++ 11及更新版本中,有std::tuple),以获得两个以上的返回结果。

随着c++ 17中结构化绑定的引入,返回std::tuple可能会成为可接受的标准。

使用结构体或类作为返回值。使用std::pair可能暂时有效,但是

如果你决定以后要返回更多信息,这是不灵活的; 从函数头中的声明来看,不太清楚返回的是什么以及返回的顺序。

Returning a structure with self-documenting member variable names will likely be less bug-prone for anyone using your function. Putting my coworker hat on for a moment, your divide_result structure is easy for me, a potential user of your function, to immediately understand after 2 seconds. Messing around with ouput parameters or mysterious pairs and tuples would take more time to read through and may be used incorrectly. And most likely even after using the function a few times I still won't remember the correct order of the arguments.