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 &'
}

Re: cannot convert from 'type' to 'type&' by John

John
Thu Nov 24 17:34:13 CST 2005

"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


Re: cannot convert from 'type' to 'type&' by Angel

Angel
Fri Nov 25 01:20:27 CST 2005


"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?


Re: cannot convert from 'type' to 'type&' by John

John
Fri Nov 25 03:08:49 CST 2005

"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


Re: cannot convert from 'type' to 'type&' by Angel

Angel
Fri Nov 25 03:33:28 CST 2005


"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.


Re: cannot convert from 'type' to 'type&' by Simon

Simon
Fri Nov 25 04:26:53 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.
>

Same error on VS 8.0 Pro edition (not beta) if you disable language
extensions.



Re: cannot convert from 'type' to 'type&' by John

John
Fri Nov 25 04:24:42 CST 2005

"Angel Tsankov" <fn42551@fmi.uni-sofia.bg> wrote in message
news:4386da67$0$41141$14726298@news.sunsite.dk
>
> 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?

VC++ 8.0 has the same problem.

It isn't the final version, but it is a "release candidate" so it is near to
final (the compiler may in fact be identical). I would be very surprised if
the problem has been fixed in the final release.

--
John Carson


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??




Re: cannot convert from 'type' to 'type&' by John

John
Fri Nov 25 05:27:55 CST 2005

"Simon Watson" <simon dot watson at sage dot com> wrote in message
news:Ojpe8ya8FHA.3132@TK2MSFTNGP12.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 &'
>>>>>> }
>
> 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??

Yes, it works for me too and it also works on VC++ 7.1. Curious, your
workaround is unrelated to the original error message of

initializing' : cannot convert from 'b' to 'b &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Good work!

--
John Carson


Re: cannot convert from 'type' to 'type&' by Tom

Tom
Fri Nov 25 07:19:58 CST 2005

John Carson wrote:
> "Simon Watson" <simon dot watson at sage dot com> wrote in message
>> 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??
>
>
> Yes, it works for me too and it also works on VC++ 7.1. Curious, your
> workaround is unrelated to the original error message of
>
> initializing' : cannot convert from 'b' to 'b &'
> A reference that is not to 'const' cannot be bound to a non-lvalue

I think the problem is that in determining the "common" type for B and D
lvalues it was coming up with B, not B& (contrary to the standardese on
?: and lvalues), and thus generating a temporary which wouldn't bind to
o. Putting in the static cast means that the types are the same, and
presumably the compiler correctly deduces B& as the type.

Tom

Re: cannot convert from 'type' to 'type&' by John

John
Fri Nov 25 09:28:30 CST 2005

"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> wrote in message
news:%23RJhwLc8FHA.4012@TK2MSFTNGP14.phx.gbl
> John Carson wrote:
>> "Simon Watson" <simon dot watson at sage dot com> wrote in message
>>> 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??
>>
>>
>> Yes, it works for me too and it also works on VC++ 7.1. Curious, your
>> workaround is unrelated to the original error message of
>>
>> initializing' : cannot convert from 'b' to 'b &'
>> A reference that is not to 'const' cannot be bound to a non-lvalue
>
> I think the problem is that in determining the "common" type for B
> and D lvalues it was coming up with B, not B& (contrary to the
> standardese on ?: and lvalues), and thus generating a temporary which
> wouldn't bind to o. Putting in the static cast means that the types
> are the same, and presumably the compiler correctly deduces B& as the
> type.
> Tom

Sounds plausible (I note, incidentally, that you have the same trouble as I
do when class names are lowercase and objects are uppercase :) ).

--
John Carson