忽略函数的按引用返回结果

发布于 2024-12-10 14:28:43 字数 797 浏览 3 评论 0原文

假设我有一个返回一个重要结果和几个不重要结果的函数。我声明了它,以便通过引用返回不重要的结果:

int CalculateStuff(int param1, int param2, int& result1, int& result2);

我想调用此函数来计算一些内容,但在调用站点我想忽略不重要的结果。我可以这样做:

...
int dummy1, dummy2;
int result = CalculateStuff(100, 42, dummy1, dummy2);
... // do something with the result

我想考虑另一种方法来做同样的事情而不声明虚拟变量:

int result = CalculateStuff(100, 42, *new int, *new int);

这有内存泄漏(不可接受),但具有比“更清楚地显示我的意图(忽略结果)的优点”虚拟”的名字。

那么,如果我这样写会发生什么:

int result = CalculateStuff(100, 42, auto_ptr(new int).get(), auto_ptr(new int).get());

合法吗?当函数代码执行时,临时整数是否仍然存在?我应该使用 unique_ptr 而不是 auto_ptr 吗?

(请不要建议重构我的代码;我可能会 - 但首先我想了解这个东西是如何工作的)

Suppose i have a function that returns an important result and several unimportant results. I declared it so that the unimportant results are returned by reference:

int CalculateStuff(int param1, int param2, int& result1, int& result2);

I would like to call this function to calculate some stuff, but at call site i want to ignore the unimportant results. I could do it as follows:

...
int dummy1, dummy2;
int result = CalculateStuff(100, 42, dummy1, dummy2);
... // do something with the result

I would like to consider another way to do the same without declaring dummy variables:

int result = CalculateStuff(100, 42, *new int, *new int);

This has a memory leak (unacceptable), but has an advantage of showing my intent (ignoring the results) more clearly than the "dummy" names.

So, what would happen if i wrote it as follows:

int result = CalculateStuff(100, 42, auto_ptr(new int).get(), auto_ptr(new int).get());

Is it legal? Will the temporary integers still exist when the function's code is executed? Should i use unique_ptr instead of auto_ptr?

(Please don't suggest refactoring my code; i probably will - but first i want to understand how this stuff works)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-12-17 14:28:44

您可以创建一个提供到 int& 的隐式(或显式)转换的类。

struct ignored
{
   int n;
   operator int&() { return n; }
};

n = CalculateStuff(a, b, ignored(), ignored());

您可以进一步将其设为模板并添加 const 重载。

You could make a class that provides implicit (or explicit) conversion to int&.

struct ignored
{
   int n;
   operator int&() { return n; }
};

n = CalculateStuff(a, b, ignored(), ignored());

You can further make this a template and add const overloads.

浮萍、无处依 2024-12-17 14:28:44

合法,但不保证不泄漏内存,见问题3
例如此处

实现可选输出的正确且惯用的方法是通过
一个指针,当你不需要结果时传递NULL,并进行测试
for NULL 在通过指针写入之前在函数中。

It's legal, but it's not guaranteed not to leak memory, see question 3
here for example.

The correct and idiomatic way of implementing optional output is to pass
a pointer, passing NULL when you don't want the results, and testing
for NULL in the function before writing through the pointer.

洋洋洒洒 2024-12-17 14:28:43

这是合法的; auto_ptr 对象将保持活动状态,直到表达式结束(即函数调用)。但它丑陋得要命。

只需重载你的函数:

int CalculateStuff(int param1, int param2, int& result1, int& result2);
int CalculateStuff(int param1, int param2) { 
    int r1=0, r2=0; 
    return CalculateStuff(param1, param2, r1, r2);
}

It is legal; auto_ptr objects will remain alive until the end of expression (i.e. function call). But it is ugly as hell.

Just overload your function:

int CalculateStuff(int param1, int param2, int& result1, int& result2);
int CalculateStuff(int param1, int param2) { 
    int r1=0, r2=0; 
    return CalculateStuff(param1, param2, r1, r2);
}
浴红衣 2024-12-17 14:28:43

根据 Bjarne Stroustrup 的说法,如果某些参数是可选的,那么将它们设置为指针类型是理想的情况,这样当您不需要为它们传递参数时可以传递 NULL:

int CalculateStuff(int param1, int param2, int * result1, int * result2);

并使用:

int result = CalculateStuff(100, 42, NULL, NULL);

所有其他替代方案都不会像这个一样好,或者至少不会比这个更好。

当然,CalculateStuff 的实现必须检查参数是否为 NULL。

According to Bjarne Stroustrup, if some parameters are optional, it is an ideal case for making them pointer type, so that you can pass NULL when you don't need to pass arguments for them:

int CalculateStuff(int param1, int param2, int * result1, int * result2);

And use:

int result = CalculateStuff(100, 42, NULL, NULL);

All other alternatives would not be as good as this one, or at least not better than this.

Of course, the implementation of CalculateStuff has to check the parameters if they're NULL or not.

绝不放开 2024-12-17 14:28:43

我的建议是:如果您必须要求 SO 弄清楚代码及其语义的正确性,那么代码无法清楚地表达您的意图

我认为第一个版本(带有 dummy1dummy2)是最透明的,并且显然是正确的。

如果您发现自己重复调用该函数并且不想要可选结果,则可以提供重载:

int CalculateStuff(int param1, int param2, int& result1, int& result2) {}

int CalculateStuff(int param1, int param2) {
  int unwanted1, unwanted2;
  return CalculateStuff(param1, param2, unwanted1, unwanted2);
}

Here is my advice: if you have to ask on SO to figure out the correctness of the code and its semantics, then the code fails at clearly expressing your intent.

I think the first version (with dummy1 and dummy2) is the most transparent, and is obviously correct.

If you find yourself repeatedly calling the function and not wanting the optional results, you could provide an overload:

int CalculateStuff(int param1, int param2, int& result1, int& result2) {}

int CalculateStuff(int param1, int param2) {
  int unwanted1, unwanted2;
  return CalculateStuff(param1, param2, unwanted1, unwanted2);
}
笑红尘 2024-12-17 14:28:43

如果您可以控制该函数,推荐的方法是:返回包含所有结果的 std::tuple (或 boost::tuple),或者编写一个不包含任何结果的重载。不需要额外的变量。

A recommended approach if you have control over the function would be: return a std::tuple (or boost::tuple) with all results or write an overload that doesn't need the extra variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文