https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70384

            Bug ID: 70384
           Summary: Unhelpful warnings on invalid initializations
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at magfr dot user.lysator.liu.se
  Target Milestone: ---

Created attachment 38074
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38074&action=edit
ex.cpp

Consider the following source (ex.cpp):
void f(int&);
void g(long t) {
        f(t);
}

Compiling this gives the error
ex.cpp: In function 'void g(long int)':
ex.cpp:3:5: error: invalid initialization of non-const reference of type 'int&'
from an rvalue of type 'int'
  f(t);
     ^
ex.cpp:1:6: note:   initializing argument 1 of 'void f(int&)'
 void f(int&);
      ^

and I think that error message is unhelpful.

invalid initialization of non-const reference of type 'int&' from an rvalue of
type 'int'

While this is true in some sense - the long input gets converted to an rvalue
int that isn't feasible as an argument to f - I still think the error message
in 4.4.6 was more helpful when it comes to correcting the error:

invalid initialization of reference of type ‘int&’ from expression of type
‘long int’

so, would it be possible to type the original type as the source type?

For reference clang++ (3.7.0) outputs
candidate function not viable: no known conversion from 'long' to 'int &' for
1st argument

and that is also helpful when it comes to fixing the problem.

Reply via email to