忽略函数的按引用返回结果
假设我有一个返回一个重要结果和几个不重要结果的函数。我声明了它,以便通过引用返回不重要的结果:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以创建一个提供到
int&
的隐式(或显式)转换的类。您可以进一步将其设为模板并添加 const 重载。
You could make a class that provides implicit (or explicit) conversion to
int&
.You can further make this a template and add const overloads.
合法,但不保证不泄漏内存,见问题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 testingfor
NULL
in the function before writing through the pointer.这是合法的;
auto_ptr
对象将保持活动状态,直到表达式结束(即函数调用)。但它丑陋得要命。只需重载你的函数:
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:
根据 Bjarne Stroustrup 的说法,如果某些参数是可选的,那么将它们设置为指针类型是理想的情况,这样当您不需要为它们传递参数时可以传递 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:And use:
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'reNULL
or not.我的建议是:如果您必须要求 SO 弄清楚代码及其语义的正确性,那么代码无法清楚地表达您的意图。
我认为第一个版本(带有
dummy1
和dummy2
)是最透明的,并且显然是正确的。如果您发现自己重复调用该函数并且不想要可选结果,则可以提供重载:
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
anddummy2
) 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:
如果您可以控制该函数,推荐的方法是:返回包含所有结果的
std::tuple
(或boost::tuple
),或者编写一个不包含任何结果的重载。不需要额外的变量。A recommended approach if you have control over the function would be: return a
std::tuple
(orboost::tuple
) with all results or write an overload that doesn't need the extra variables.