public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32525]  New: Request for new warning: useless dynamic_casts
@ 2007-06-27 18:08 lloyd at randombit dot net
  2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: lloyd at randombit dot net @ 2007-06-27 18:08 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-02-21  0:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-27 18:08 [Bug c++/32525] New: Request for new warning: useless dynamic_casts lloyd at randombit dot net
2007-06-27 18:14 ` [Bug c++/32525] " pinskia at gcc dot gnu dot org
2007-06-27 18:54 ` bangerth at dealii dot org
2007-06-27 19:06 ` lloyd at randombit dot net
2007-07-14 13:02   ` Gabriel Dos Reis
2007-06-27 19:17 ` bangerth at dealii dot org
2007-06-27 19:33 ` lloyd at randombit dot net
2007-07-14 13:02 ` gdr at cs dot tamu dot edu
2007-07-15  0:04 ` sebor at roguewave dot com
2010-02-21  0:17 ` manu at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).