Re: RVO and visiblity
On 30 Okt., 00:27, "andrew.bell...@gmail.com"
<andrew.bell...@gmail.com> wrote:
Is there any way that return value optimization be done if the
implmentation of the callee is not visible to the compiler at the
point of the call?
Sure. All you have to do is using an ABI with a return value passing
protocol that *allows* optional RVO inside a function's
implementation. I'm no compiler expert but I would make the
"protocol" only dependent on the return type. For example, return
values for scalar types and small class types which are easily
copiable via memcpy (no user-defined copy ctors involved) could be
returned via CPU registers. If the return type T is too large or not
copiable via memcpy the binary interface could turn a function like
this
T some_function();
into a function like this (low level):
void some_function(void* retptr)
and let the function do a "placement new".
Note: A declaration of such a function is allowed even in case T is
incomplete. But by the time you want to call it, T must be a complete
type. So, the compiler can determine the correct binary interface
depending on the known properties of T alone.
This binary interface supports RVO but doesn't require it. For
example, a compiler can transform the following function
T some_function();
{
T ret (23);
ret.foo(99);
return ret;
}
into
void some_function(void* retptr)
{
T& ret = *new(retptr) T(23);
ret.foo(99);
}
where the unoptimized version would look like this
void some_function(void* retptr)
{
T ret (23);
ret.foo(99);
new(retptr) T(ret); // placement copy-construct
}
The call site always makes room for the object and passes a pointer to
it to the function regardless of whether RVO is applied or not.
HTH,
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]