Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jarthur!petunia!kestrel.edu!gyro From: gyro@kestrel.edu (Scott Layson Burson) Newsgroups: comp.lang.c++ Subject: Re: implicit conversion from a reference Message-ID: <1991May4.040222.12992@kestrel.edu> Date: 4 May 91 04:02:22 GMT References: <1991May1.143350.19206@roundup.crhc.uiuc.edu> <1991May2.142710.7034@clear.com> Organization: Kestrel Institute, Palo Alto, CA Lines: 46 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