从函数返回结构 [英] Returning a Struct from a Function

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

问题描述

您好,新手在这里。


我想知道是否有办法从

函数返回结构,而不创建任何其他中间变量。此外,

应该这样做,使得函数仍然可以声明为

inline。


更确切地说,给定结构:


struct TwoFields {

int Field_one;

int Field_two;

}


我想要一个类似下面的函数,但没有

额外声明,TwoFields myReturnStruct;:


inline TwoFields ReturnMyFields(int myInt){


TwoFields myReturnStruct;


myReturnStruct.Field_one = myInt / 2;

myReturnStruct.Field_two = myInt%2;


返回myReturnStruct;

}


唯一我希望这是速度效率的原因,所以如果这不是值得做的话请告诉我。


Alex

解决方案

al*******@gmail.com dixit :


您好,这里有新手。


我想知道是否有办法从

函数返回结构,而不创建任何其他中间变量。此外,

应该这样做,使得该函数仍然可以声明为

inline。



通常的方法是将

中的结构(作为参数)的引用传递给你想要放回的值(对于vector,list,...来说是一样的)


5月11日下午2:02,john< j ... @ gmail.comwrote:


alex.j. .. @ gmail.com dixit:


您好,新手在这里。


我想知道是否有办法从

函数返回结构,而不创建任何其他中间变量。此外,

应该这样做,使得该函数仍然可以声明为

inline。



通常的方法是将

中的结构(作为参数)的引用传递给你想要的返回值(同样的矢量,列表,...)



好​​的,这当然有效。


但这是唯一的这样做的方法?


如果我有很多代码通过创建一个中间变量返回一个struct(或vector,

等...)怎么办?在功能;而且我确实

不想修改代码中所有出现的函数,

只有函数本身。


我可以消除额外变量的创建,同时返回

结构(但不作为参数)?


另外,感谢快速回复。


Alex


On 11 Maj,19:57,alex.j ... @ gmail.com写道:


您好,新手在这里。


*我想知道是否有办法从a返回结构

函数,无需创建任何其他中间变量。此外,

应该这样做,使得该函数仍然可以声明为

inline。



是的。任何普通的功能都可能会这样做。编译器允许使用

来优化这个特殊情况,该特殊情况已被赋予名称(N)RVO

(命名)返回值优化和所有最近的编译器应该是

能够使用此策略。


>

*更确切地说,给定结构:


struct TwoFields {

* * int Field_one;

* * int Field_two;


}


我想要一个类似下面的函数,但没有

额外声明,TwoFields myReturnStruct;:


inline TwoFields ReturnMyFields(int myInt) {$ / $

TwoFields myReturnStruct;


myReturnStruct.Field_one = myInt / 2;

myReturnStruct.Field_two = myInt% 2;


返回myReturnStruct;


}



这看起来很好我。


*我想要的唯一原因是速度效率,所以如果这不是值得做的话请告诉我。



我可以同情你的态度 - 我曾经这么认为

我自己。但是严肃地说:你是否相信执行中的差异

时间对于这个功能会有什么不同?


/ Peter


Hello, newbie here.

I''d like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

More precisely, given the structure:

struct TwoFields {
int Field_one;
int Field_two;
}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;
}

The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.

Alex

解决方案

al*******@gmail.com dixit:

Hello, newbie here.

I''d like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

The usual way is to pass a reference to the struct (as a parameter) in
which you want to put the return value (the same for vector, list, ...)


On May 11, 2:02 pm, john <j...@gmail.comwrote:

alex.j...@gmail.com dixit:

Hello, newbie here.

I''d like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.


The usual way is to pass a reference to the struct (as a parameter) in
which you want to put the return value (the same for vector, list, ...)

OK, that certainly works.

But is this the only way to do it?

What if I have a lot of code which returns a struct (or vector,
etc..) by creating an intermediate variable in the function; and I do
not want to modify all the occurrences of the function in the code,
only the function itself.

Can I eliminate the creation of the extra variable, while returning
the struct (but not as a parameter) ?

Also, thanks for the quick reply.

Alex


On 11 Maj, 19:57, alex.j...@gmail.com wrote:

Hello, newbie here.

* I''d like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

Yes. Any ordinary function will likely do so. The compiler is allowed
to optimise this special case that has been given the name (N)RVO for
(Named) Return Value Optimisation and all recent compilers should be
able to use this strategy.

>
* More precisely, given the structure:

struct TwoFields {
* *int Field_one;
* *int Field_two;

}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;

}

This looks fine to me.

* The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.

I can sympathise with your attitude - I used to think that way
myself. But seriously: do you believe that the difference in execution
time for that function is going to make any difference?

/Peter


这篇关于从函数返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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