Re: cannot convert from 'type' to 'type&' by Simon
Simon
Fri Nov 25 04:41:16 CST 2005
"Angel Tsankov" <fn42551@fmi.uni-sofia.bg> wrote in message
news:4386da67$0$41141$14726298@news.sunsite.dk...
>
> "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
> news:ermh5$Z8FHA.952@TK2MSFTNGP12.phx.gbl...
> > "Angel Tsankov" <fn42551@fmi.uni-sofia.bg> wrote in message
> > news:4386bb3a$0$41137$14726298@news.sunsite.dk
> >> "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
> >> news:u8G6Y%23U8FHA.956@TK2MSFTNGP10.phx.gbl...
> >>> "Angel Tsankov" <fn42551@fmi.uni-sofia.bg> wrote in message
> >>> news:u1OWWFT8FHA.1292@tk2msftngp13.phx.gbl
> >>>> What is wrong with this code:
> >>>>
> >>>> class b
> >>>> {
> >>>> };
> >>>>
> >>>> class d : public b
> >>>> {
> >>>> };
> >>>>
> >>>> int main
> >>>> (
> >>>> int num_args
> >>>> , char* args[]
> >>>> )
> >>>> {
> >>>> b B;
> >>>> d D;
> >>>> b& br = B;
> >>>> d& dr = D;
> >>>> b& o = num_args == 1 ? br : dr; //error C2440: 'initializing' :
> >>>> cannot convert from 'b' to 'b &'
> >>>> }
> >>>
> >>> Nothing is wrong that I can see. It compiles fine for me on
> >>> VC++7.1
> >>> and 8; also on Comeau online.
> >>>
> >>> --
> >>> John Carson
> >>
> >> Does it!? Did you disable language extensions on VC++ 7.1? If so,
> >> could you send the project file?
> >
> > No, I didn't disable language extensions. When I do, it produces an
> > error.
> > This, however, seems to be a bug. The fact that Comeau compiles it
> > tells you
> > with 99.999% probability that the code is correct.
> >
> > The error message says in full:
> >
> > initializing' : cannot convert from 'b' to 'b &'
> > A reference that is not to 'const' cannot be bound to a non-lvalue
> >
> > This seems wrong since, as far as I can see, there are no
> > non-lvalues in the code. Indeed, the following code compiles on
> > VC++7.1 (language extensions disabled) without a problem:
> >
> > class b
> > {
> > };
> >
> > class d : public b
> > {
> > };
> >
> > int main
> > (
> > int num_args
> > , char* args[]
> > )
> > {
> > b B, B1;
> > d D, D1;
> > b& br = B;
> > d& dr = D;
> > br = B1; // br is an lvalue apparently
> > dr = D1; // as is dr
> > }
> >
> > (this use of lower case names for classes and upper class names for
> > objects
> > is sending my head spinning).
> >
> > Hardly anyone disables language extensions. Thus, when you do, you
> > should
> > think of yourself as a beta tester.
> >
> >
> > --
> > John Carson
> >
>
> Well, in fact I disable language extensions when I write code that is
> expected to be portable. Both g++ 3.4.4 and g++ 4.0.0 compile the code
> w/o problems. Unfortunately, VC++ 7.1 does not. What about VC++ 8.0?
> Btw, is it a beta or final?
>
> P.S. Sorry, for the mess of upper- and lower-case identifiers.
>
This however, does work
b& o = num_args == 1 ? br : static_cast<b&>(dr); //no error
It looks like the conditional operator wants both choices to be of the same
type. I wonder if the VC compiler thinks it needs to do to many implicit
type conversions??