Home > Archive > VC Language > November 2005 > cannot convert from 'type' to 'type&'
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
cannot convert from 'type' to 'type&'
|
|
| Angel Tsankov 2005-11-24, 7:01 pm |
| 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 &'
}
| |
| John Carson 2005-11-24, 7:01 pm |
| "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
| |
| Angel Tsankov 2005-11-25, 3:59 am |
|
"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
>
> 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?
| |
| John Carson 2005-11-25, 3:59 am |
| "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...
>
> 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
| |
| Angel Tsankov 2005-11-25, 3:59 am |
|
"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
>
> 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.
| |
| Simon Watson 2005-11-25, 7:58 am |
|
"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...
>
> 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.
| |
| John Carson 2005-11-25, 7:58 am |
| "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
| |
| Simon Watson 2005-11-25, 7:58 am |
|
"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...
>
> 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??
| |
| John Carson 2005-11-25, 7:58 am |
| "Simon Watson" <simon dot watson at sage dot com> wrote in message
news:Ojpe8ya8FHA.3132@TK2MSFTNGP12.phx.gbl
>
> 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
| |
| Tom Widmer [VC++ MVP] 2005-11-25, 7:58 am |
| John Carson wrote:
> "Simon Watson" <simon dot watson at sage dot com> wrote in message
>
>
> 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
| |
| John Carson 2005-11-25, 7:02 pm |
| "Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> wrote in message
news:%23RJhwLc8FHA.4012@TK2MSFTNGP14.phx.gbl
> John Carson wrote:
>
> 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
|
|
|
|
|