Boost logo

Boost :

From: Raoul Gough (RaoulGough_at_[hidden])
Date: 2004-02-09 14:49:25


"Rani Sharoni" <rani_sharoni_at_[hidden]> writes:

[snip]
> The fact that the additional temporary is const might,
> theoretically, encourage the optimizer to generate faster code but
> AFAIK no C++ optimizer is actually aware about the existence of C++
> const and *ignore* it.

So I can't cast away const on the reference, because it might be bound
to a const copy of the rvalue (leading to undefined behaviour)?

BTW, couldn't the optimizer (after verifying that the const & is never
const-casted) perform the same optimizations anyway?

> Anyway, we don't really care about this elided coping constraint
> since if compiler actually makes additional coping then it probably
> has very good reason: X const& r = true ? X() : X(); // VC and GCC
> introduce additional r-value using the "r-value" move constructor.

The funny thing about the conditional expression is that neither VC++
7.1 nor g++ 3.3.1 can compile the case with an rvalue-to-lvalue
conversion in the following code:

class nc {
  nc (nc const &) { }
public:
  nc () { }
  nc const &by_ref () const { return *this; }
};

struct nc_derived : nc { };

void bar (nc const &);

void foo (bool b) {
  bar (b ? nc() : nc_derived()); // Ill-formed (8.5.3)
  bar ((b ? nc() : nc_derived()).by_ref()); // OK AFAIK
}

Both VC++ and g++ report that the nc copy constructor is inaccessible
on both of the bar calls. I don't see how the compiler is allowed to
introduce a copy in the second one (with the member function call).
Comeau online 4.3.3 diagnoses the error on the ill-formed line and
accepts the second one.

-- 
Raoul Gough.
export LESS='-X'

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