Boost logo

Boost :

From: Anthony Williams (anthwil_at_[hidden])
Date: 2002-10-01 03:46:12


Gennadiy Rozental writes:
> > Difficult but I'll try. Rvalues of non-class types don't have addresses as
> > they can live outside the addressable memory (in registers, for example.)
> > When a reference is bound to an rvalue, that rvalue may need to be copied
> to
> > addressable memory. In a context where 'this' is not used, rvalues of
> class
> > types can live outside addressable memory, too.
>
> Does this mean that if 'this' IS used than compiler is not allowed to put
> rvalues of class types outside addressable memory?

 You can call member functions on rvalues of class type. If the member
 function uses this, then it must be addressable.
 
> > So the compiler in the above is allowed to use the copy constructor to
> > create another (addressable) temporary of type 'A' (or 'A const') and bind
> > the reference to it, instead of binding directly to 'A()' that may not be
> > addressable.
> Continue my question: does the following should compile?
>
> class A {
> public:
> A() {};
> int field;
> private:
> A( A const& );
> void operator=( A const& );
> };
>
> void foo( A const& a )
> {
> int i = a.field;
> }
>
> void moo()
> {
> foo( A() );
> }

No. The copy constructor must be accessible, so the temporary A() can be bound
to the const reference. Note that in doing such a binding, the compiler may
make an _arbitrary_ number of copies (e.g. 10000000) if it feels like it, or
it may be able to omit all copies. To cater for the first case, the copy
constructor _must_ be accessible. If the compiler doesn't actually make a
copy, it doesn't have to be defined though.

Anthony


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