This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/32525] New: Request for new warning: useless dynamic_casts


It might be useful for GCC to warn about dynamic_casts which are not necessary.
For instance a dynamic_cast<T*>(T*), or a dynamic_cast from a derived class to
a base class (there might be some corner cases here with multiple
inheritance?). I do see that such dynamic_casts are no-op'ed away (even without
any optimization flags! (at least in my toy test program)), which is certainly
positive. However it would be nice if the programmer was notified about them,
since even if there is no run-time cost, there is a source-level increase in
complexity which can easily be avoided (and there may well be run-time costs
involved with other compilers).

A quick example of the sort of thing I have in mind:

class base
{
  public:
    virtual int f() = 0;
    virtual ~base() {}
};

class derived : public base
{
  public:
    int f() { return 1; }
};

#include <stdio.h>

int main()
{
    derived* obj = new derived();
    base* baseptr = dynamic_cast<base*>(obj); // warn: to a base class
    derived* sametype = dynamic_cast<derived*>(obj); // warn: same type
    derived* from_base = dynamic_cast<derived*>(baseptr); // ok

    printf("%d %d %d %d\n",
           obj->f(), baseptr->f(), sametype->f(), from_base->f());
}

(Compiling this on x86-64 shows GCC 4.1.0 is no-op'ing the first two
dynamic_casts, with or without optimization).


-- 
           Summary: Request for new warning: useless dynamic_casts
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lloyd at randombit dot net


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]