[comp.lang.c++] implicit conversion from a reference

gyro@kestrel.edu (Scott Layson Burson) (05/04/91)

In article <1991May2.142710.7034@clear.com> rmartin@clear.com (Bob Martin) writes:
>In article <1991May1.143350.19206@roundup.crhc.uiuc.edu> steven@pacific.csl.uiuc.edu writes:
>>Under cfront 2.0, in the following program, x does not seem to convert to
>>an (int&) the way that we think it should.  
>>
>>class Int {
>>
>>  public:
>>    Int () {}
>>    Int ( int x ) : me ( x ) {}
>>    operator int& () { return me; }
>>    int* operator & () { return &me; }
>>
>>  private:
>>    int me;
>>};
>>
>>int f(int x ) { return x; }
>>
>>main()
>>{
>>    Int x;
>>    x=10;		// works
>>    x+=5;		// works
>>    x = * &x;		// works
>>    f(x);		// this doesn't work ...
>>    f( (int&) x);	// ... but this does
>>}
>
>It appears that the compiler is invoking the rule as stated in 12.3.2 of
>the ARM: "At most one user-defined conversion (Constructor or
>conversion function) is implicitly applied to a single value."  Thus
>the compiler is willing to convert Int to int& but not willing to
>convert Int to int since that would take two implicit steps.

I don't understand this.  The conversion from `int&' to `int' is not
a user-defined conversion.  I can find no rule in the ARM that
explains why `Int' cannot be converted to `int'.

However, G++ 1.37.1 doesn't like it either, so it's hard to believe
it's a bug.  Then again, G++ doesn't like the line `x += 5;' either.

Can anyone explain what's going on here?

-- Scott
Gyro@Reasoning.COM

gyro@kestrel.edu (Scott Layson Burson) (05/24/91)

In article <1991May9.155949.25063@clear.com> rmartin@clear.com (Bob Martin) writes:
>In article <1991May4.040222.12992@kestrel.edu> 
>	gyro@kestrel.edu (Scott Layson Burson) writes:
>
>>In article <1991May2.142710.7034@clear.com> 
>	rmartin@clear.com (Bob Martin) writes:
>
>>>It appears that the compiler is invoking the rule as stated in 12.3.2 of
>>>the ARM: "At most one user-defined conversion (Constructor or
>>>conversion function) is implicitly applied to a single value."  Thus
>>>the compiler is willing to convert Int to int& but not willing to
>>>convert Int to int since that would take two implicit steps.
>>
>>I don't understand this.  The conversion from `int&' to `int' is not
>>a user-defined conversion.  I can find no rule in the ARM that
>>explains why `Int' cannot be converted to `int'.
>>
>>However, G++ 1.37.1 doesn't like it either, so it's hard to believe
>>it's a bug.  Then again, G++ doesn't like the line `x += 5;' either.
>>
>>Can anyone explain what's going on here?
>
>Scott:
>One way to interpret the rule quoted above is that the compiler will
>either apply some implicit conversions to an object.  Or it will
>apply at most ONE user defined conversion implicitly.

That's not what I take to be the plain meaning of the sentence quoted
above.  Seems to me that either the rule is very poorly worded, or it
has been misinterpreted.

I will see if I can get clarification from the committee.

-- Scott
Gyro@Reasoning.COM