Boost :: Tuples vs Structs返回值 [英] Boost::Tuples vs Structs for return values

查看:188
本文介绍了Boost :: Tuples vs Structs返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的头围绕元组(感谢@litb),和他们使用的常见建议是返回> 1值的函数。



这是我通常使用的结构体,我不能理解元组的优势在这种情况下 - 看起来是一个容易出错的方法最终懒惰。



借用一个例子,我会使用这个

  struct divide_result {
int quotient;
int remainder;
};

使用元组,你会有

  typedef boost :: tuple< int,int> divide_result; 

但是,如果没有读取你正在调用的函数的代码蠢到信任他们)你不知道哪个int是商,反之亦然。它看起来像... ...

  struct divide_result {
int results [2] // 0是商,1是余数,我认为
};

...这不会让我充满信心。



那么,

是元组对于弥补歧义的结构的优点。

解决方案

tuples



我认为我同意你的问题,什么位置对应什么变量可能会引起混乱。但我认为有两方面。一个是呼叫方,另一个是被叫方

  int remainder; 
int quotient;
tie(quotient,remainder)= div(10,3);

我认为这很清楚我们得到了什么,但是如果你必须返回更多的值立刻。一旦调用程序员查找了 div 的文档,他就知道什么位置是什么,并且可以编写有效的代码。作为一个经验法则,我会说不要一次返回多于4个值。对于任何超越,喜欢结构。



输出参数



输出参数也可以使用,当然:

  int remainder; 
int quotient;
div(10,3,& quotient,& remainder);

现在我想说明元组如何比输出参数更好。我们已经将 div 的输入与其输出混合,而没有获得任何优势。更糟的是,我们让那些代码的读者怀疑 返回值 div 是什么。 是输出参数有用的例子。在我看来,你应该使用它们,只有当你没有其他方式,因为返回值已经采取,不能更改为元组或结构。 operator>> 是使用输出参数的一个很好的例子,因为返回值已经为流保留,因此可以链接 operator> ;> 呼叫。如果你不使用运算符,并且上下文不是很清楚,我建议你使用指针,在调用端发信号通知对象实际上用作输出参数,除了适当的注释。 / p>

返回一个结构



第三个选项是使用结构:

  div_result d = div(10,3); 

我认为肯定会赢得清晰但是请注意,您仍然需要访问该结构中的结果,并且结果不会表露在表上,因为输出参数和使用 tie



我认为最重要的一点是让一切尽可能通用。所以,假设你有一个函数可以打印出元组。你可以只做

  cout< div(10,3); 

并显示您的结果。我认为元组,另一方面,显然赢得他们多才多艺的性质。使用div_result做,你需要重载operator<<,或需要单独输出每个成员。


I'm trying to get my head around tuples (thanks @litb), and the common suggestion for their use is for functions returning > 1 value.

This is something that I'd normally use a struct for , and I can't understand the advantages to tuples in this case - it seems an error-prone approach for the terminally lazy.

Borrowing an example, I'd use this

struct divide_result {
    int quotient;
    int remainder;
};

Using a tuple, you'd have

typedef boost::tuple<int, int> divide_result;

But without reading the code of the function you're calling (or the comments, if you're dumb enough to trust them) you have no idea which int is quotient and vice-versa. It seems rather like...

struct divide_result {
    int results[2]; // 0 is quotient, 1 is remainder, I think
};

...which wouldn't fill me with confidence.

So, what are the advantages of tuples over structs that compensate for the ambiguity?

解决方案

tuples

I think i agree with you that the issue with what position corresponds to what variable can introduce confusion. But i think there are two sides. One is the call-side and the other is the callee-side:

int remainder; 
int quotient;
tie(quotient, remainder) = div(10, 3);

I think it's crystal clear what we got, but it can become confusing if you have to return more values at once. Once the caller's programmer has looked up the documentation of div, he will know what position is what, and can write effective code. As a rule of thumb, i would say not to return more than 4 values at once. For anything beyond, prefer a struct.

output parameters

Output parameters can be used too, of course:

int remainder; 
int quotient;
div(10, 3, &quotient, &remainder);

Now i think that illustrates how tuples are better than output parameters. We have mixed the input of div with its output, while not gaining any advantage. Worse, we leave the reader of that code in doubt on what could be the actual return value of div be. There are wonderful examples when output parameters are useful. In my opinion, you should use them only when you've got no other way, because the return value is already taken and can't be changed to either a tuple or struct. operator>> is a good example on where you use output parameters, because the return value is already reserved for the stream, so you can chain operator>> calls. If you've not to do with operators, and the context is not crystal clear, i recommend you to use pointers, to signal at the call side that the object is actually used as an output parameter, in addition to comments where appropriate.

returning a struct

The third option is to use a struct:

div_result d = div(10, 3);

I think that definitely wins the award for clearness. But note you have still to access the result within that struct, and the result is not "laid bare" on the table, as it was the case for the output parameters and the tuple used with tie.

I think a major point these days is to make everything as generic as possible. So, say you have got a function that can print out tuples. You can just do

cout << div(10, 3);

And have your result displayed. I think that tuples, on the other side, clearly win for their versatile nature. Doing that with div_result, you need to overload operator<<, or need to output each member separately.

这篇关于Boost :: Tuples vs Structs返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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