"BigMan" <BigMan@[EMAIL PROTECTED]
> schrieb im Newsbeitrag
news:bd1f0b04.0502010042.78585f5b@[EMAIL PROTECTED]
> I get this error message
>
> "error C2440: 'argument' : cannot convert from 'Type' to 'Type &'"
>
> compiling the following piece of code with VC++ 7.1 (w/o language
> extensions!):
>
>
>
///////////////////////////////////////////////////////////////////////////
>
> class Type
> {
> public:
> Type ( ) { }
> Type( Type& ) { }
> };
>
>
////////////////////////////////////////////////////////////////////////////
///
>
> inline Type operator +
> (
> Type const&
> , Type const&
> )
> {
> Type Result;
> return Result;
> }
>
>
///////////////////////////////////////////////////////////////////////////
>
> inline Type operator -
> (
> Type const&,
> Type const&
> )
> {
> Type Result;
> return Result;
> }
>
>
////////////////////////////////////////////////////////////////////////////
///
>
> int main
> (
> int,
> char*
> )
Note this is not one of the guaranteed signatures of main.
> {
> Type a, b, c;
> a - b; // 1
> a - b + c; // 2
>
> return 0;
> }
>
>
////////////////////////////////////////////////////////////////////////////
///
>
> Line 1 compiles fine, but line 2 does not. VC++ 7.1 tries to call the
> copy ctor in order to compile line 2 and fails. I cannot see the
> reason to do so.
It's illegal for some rather obscure reason. a,b, and c are lvalues and
bind
directly to the call of the operator- in line 1. For line2, the first
arithmetic expression returns a Type by value, which is an rvalue. Rvalues
can be bound to reference to const (your operator+), however the
implementation is allowed to make a copy of the rvalue and bind that copy
to
the reference instead of binding the original rvalue directly to the
reference. I am not aware of any implementation that does so (but I have
never cared a lot about it), but the Standard says clearly even if the
copy
is elided, the copy constructor must be callable and valid. But for the
copy
constructor, the reference is to non-const, but you cannot bind an rvalue
to
reference to non-const.
This makes the call illegal. There is nothing special about using
operator+
or operator-, it's because the copy of the rvalue is allowed, but your
copy-constructor doesn't suit rvalues.
Is there some special reason why your copy constructor takes a reference
to
non-const?
BTW, GCC 3.3.1 eats the code happily. Can anyone confirm if this is true
for
the newest release version too? If yes, I'll file a bug re****t.
>
> Please tell me if this code sould compile accoring to the standard. If
> not, explain why.
See the details in 8.5.3/5.
Thomas
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|