[Bug c++/88503] New: 'invalid static_cast' error message could be more helpful

petschy at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Dec 14 21:39:00 GMT 2018


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

            Bug ID: 88503
           Summary: 'invalid static_cast' error message could be more
                    helpful
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: petschy at gmail dot com
  Target Milestone: ---

class Parent;
class Derived;

Derived* foo(Parent* p)
{
        return static_cast<Derived*>(p);
}

$ g++-9.0.0 -Wall -Wextra -c 20181214-fwddecl_vs_static_cast.cpp 
20181214-fwddecl_vs_static_cast.cpp: In function ‘Derived* foo(Parent*)’:
20181214-fwddecl_vs_static_cast.cpp:6:32: error: invalid static_cast from type
‘Parent*’ to type ‘Derived*’
    6 |  return static_cast<Derived*>(p);
      |                                ^


Now imagine that your project is somewhat more complex than the above example,
and Derived derives from Parent. You know it, you are used to it. However, some
code needs only the forward declarations, at other places the full definition
is needed.

When I bumped into this error, it caused me quite some time to figure out what
is going on. I double/triple/quad checked the names of the classes, no typo,
yet the error. It turned out of course that only the forward declarations were
available in that TU.

It would be really useful if the error message was a bit more verbose,
mentioning the fact if either types were forward declared, and if no
parent-child relationship info is available.


More information about the Gcc-bugs mailing list