Boost logo

Boost :

From: Vertleyb (vertleyb_at_[hidden])
Date: 2006-02-11 20:55:12


Hi Jaakko,

> I think the extended lifetime rule for temporaries would make it work
> ok, but
> the mere definition of the "forward" function above will at least
> trigger warnings about
> returning a reference to a temporary.
>
> (I'm assuming you meant "const x(*f)()" as the parameter of the
> forward function)

Right.

Should have verified it myself rather than just speculating :-(

However, this problem seems to be easily remedied with assigning to an intermediate variable. The following compiles w/o warnings,
and runs correctly, on mingw 3.4.2, and VC7.1/8.0 (warning level 4):

#include <iostream>
using namespace std;

const int f()
{
    return 0;
}
const int& ff()
{
    const int& result = f();
    return result;
}
int main()
{
    const int& r = ff();
    cout << r << endl;
    return 0;
}

I am still not sure though, if the lifetime is extended far enough for this case, or does this just work correctly by accident...
(have to take a closer look into the Standard)

Regards,
Arkadiy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk