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++/47940] warn about calls to a pure virtual from a constructor/destructor


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

--- Comment #6 from mlg <mlg7 at yandex dot ru> 2011-03-01 17:23:41 UTC ---
(In reply to comment #4)
> (In reply to comment #0)
> > 
> > Functions that call pure virtual functions cannot
> > be called from constructors and destructors.
> > This may be discovered at compile-time, and the above
> > text makes a good error/warning message.
> 
> I'm not sure it makes a good diagnostic, consider:
> 
> struct abc {
>   abc(bool nasal_demons) { if (nasal_demons) fly(); }
>   void fly() { doFly(); }
>   virtual void doFly() = 0;
> };
>

That is the most dangerous situation: the crashing call is
buried [deep] in the control flow.
The deeper it is buried, the more valuable a warning/error
message would be.

Ok, "a pure virtual function may be called indirectly 
from a constructor/destructor"

> Ideally the diagnostic for this would say "may call a pure virtual" for cases
> where it can't be determined.

Probably you are right, a kind of "I know what I'm doing"
pragma would be useful as well.

I also want to note that the top-level function may be virtual:
[NOTE: usefunc() is virtual here]
==byebug2.cpp==
#include <stdio.h>
class Base {
public:
    ~Base() { usefunc(); }
    virtual void func()=0;
    virtual void usefunc() { func(); }
};

class Derived: public Base {
public:
    virtual void func() {}
};

int main() {
    Derived d;
    printf("life is good\n");
    return 0;
}
==eof==
$ g++ -Wall -Wextra byebug2.cpp
$ ./a.out 
life is good
pure virtual method called
terminate called without an active exception
Aborted


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