Boost logo

Boost :

From: Rani Sharoni (rani_sharoni_at_[hidden])
Date: 2004-02-10 05:50:08


Anthony Williams wrote:
> David Abrahams <dave_at_[hidden]> writes:
>> I don't see any problem here. There's no problem getting the rvalue
>> out of source(), or sink(source()) would fail. Furthermore, no
>> compiler I've ever seen will actually make the allowed temporary in
>> that case.
>
> How about
> [...]
> const X& x=ySource().x;
>
> This strikes me as more likely to cause a copy than a
> straight-forward binding from an X temporary.

I tried your suggestion on several compliers.

EDG and BCC didn't copy.
VC copied using the copy ctor.
GCC3.2 copied *without* using the copy ctor!

<code>
extern "C" int printf(const char*, ...);

struct X {
    X() {}
    X(X const&) { printf("X(X const&)\n"); }
};

struct Y {
    X arr[1000];
    X x;

    ~Y() { printf("~Y(): %p\n", &x); }
};

int main() {
    const X& x = Y().x;
    printf("X: %p\n", &x);
    return 0;
}

#if 0

VC:
X(X const&)
~Y(): 0012FF74
X: 0012FB88

GCC:
~Y(): 0x22fec0
X: 0x22fec8

Comeau:
X: 0012FE7C
~Y(): 0012FE7C

BCC:
X: 0012FF84
~Y(): 0012FF84

#endif


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