------- Comment #9 from jwakely dot gcc at gmail dot com  2009-08-05 10:03 
-------
This example has four slightly different errors:

struct B {}; 
struct D : B {}; 

struct X {};

int foo(B*&);

int i = foo( (B*)0 );

int j = foo( (D*)0 );

D* pd = 0;
int k = foo( pd );

X* px = 0;
int l = foo( px );

GCC 4.4.1 gives:

pr13979.cc:8:20: error: invalid initialization of non-const reference of type
‘B*&’ from a temporary of type ‘B*’
pr13979.cc:6:5: error: in passing argument 1 of ‘int foo(B*&)’
pr13979.cc:10:20: error: invalid initialization of non-const reference of type
‘B*&’ from a temporary of type ‘D*’
pr13979.cc:6:5: error: in passing argument 1 of ‘int foo(B*&)’
pr13979.cc:13:17: error: invalid initialization of reference of type ‘B*&’ from
expression of type ‘D*’
pr13979.cc:6:5: error: in passing argument 1 of ‘int foo(B*&)’
pr13979.cc:16:17: error: invalid initialization of reference of type ‘B*&’ from
expression of type ‘X*’
pr13979.cc:6:5: error: in passing argument 1 of ‘int foo(B*&)’

Comeau gives equivalent errors in all four cases. The only significant
difference is that Comeau doesn't mention the types in the first two errors.

GCC 2.95.2 is quite different:

pr13979.cc:8: initialization of non-const reference type `struct B *&'
pr13979.cc:8: from rvalue of type `B *'
pr13979.cc:6: in passing argument 1 of `foo(B *&)'
pr13979.cc:10: initializing non-const `B *&' with `D *' will use a temporary
pr13979.cc:6: in passing argument 1 of `foo(B *&)'
pr13979.cc:13: initializing non-const `B *&' with `D *' will use a temporary
pr13979.cc:6: in passing argument 1 of `foo(B *&)'
pr13979.cc:16: type `B' is not a base type for type `X'
pr13979.cc:6: in passing argument 1 of `foo(B *&)'

I think the current diagnostics are better than the 2.95 ones.  As I said
above, "will use a temporary" is misleading.  The fourth error saying "is not a
base type" may be true, but is also misleading, because it wouldn't compile
even if it was a base type.

As I said earlier, it might be nice if the third error said e.g.

pr13979.cc:13:17: error: invalid initialization of reference of type ‘B*&’ from
expression of type ‘D*’
pr13979.cc:6:5: error: in passing argument 1 of ‘int foo(B*&)’
pr13979.cc:13:17: note: conversion from `D*' to `B*' would create a temporary

However,

(In reply to comment #8)
> I don't even know if we have different codepaths for those!

if it's not possible then I think the current diagnostics are reasonable, and
at least as good as Comeau's.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13979

Reply via email to