Your second version tells the compiler to use atype_t's copy constructor
to make a copy of the object and then get a and b from that copy.
After earlier compiler steps generate an intermediate level
representation of that copy constructor, the optimizer might well figure
out that all the copying is redundant and eliminate it and generate the
minimal code which you want.
All that assumes the compiler really does take your suggestion of
"inline". As someone else already mentioned, "inline" is just a
suggestion. If the code weren't inlined then the optimizer could not
avoid the copy operation.
I think it is cleanest to pass by const reference, assuming the real
code, like your reduced example, only reads the structure, and assuming
we are talking about C++, not C.
inline int
sum(atype_t const& x)
{
return x.a + x.b;
}
That gives the compiler the least opportunity to generate bad code.
Maybe all versions discussed will generate equal code, but why push your
luck.
VM wrote:
Hello,
I'm trying to decide on the best way to pass parameters to inline
function. For example, the two functions below:
inline int
sum(atype_t *x)
{
return x->a + x->b;
}
and
inline int
sum(atype_t x)
{
return x.a + x.b;
}
Since the function is inline, my guess is that the compiler will
generate identical code for both. So there should be no performance
difference.
Is this assumption correct?
V