Lets assume i use Visual Studio or modern GCC with -O2. Will compiler create S
inside func()
and then copy it to a my_result
, or will it create my_result
with constructor (5, 6, 5 + 6)
without creating temporary S
?
NOTE: Function func()
definition and its usage are in separate .obj files!
struct S
{
S(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
int x, y, z;
};
S func(int a, int b)
{
return S(a, b, a + b);
}
/// USAGE ///
S my_result = func( 5, 6 );
Modern compilers will often optimize this kind of operation. See return value optimization