This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Instantiation of pure virtual class
- To: egcs-bugs at egcs dot cygnus dot com, Sunil Rottoo <sunil at gdc dot ca>
- Subject: Instantiation of pure virtual class
- From: Roger Leblanc <rleblanc at gdc dot ca>
- Date: Fri, 25 Feb 2000 12:04:24 -0500
- Organization: General DataComm
Hi,
I think I found a bug in g++ version 2.95.2 running on a Intel Linux box
(Red Hat 6.1).
Consider the following code:
#include <iostream>
#include <list>
#include <algo.h>
#include <assert.h>
// Pure virtual class
struct BasePredicate
{
virtual bool operator ()(int a) = 0;
};
struct DerivedPredicate : public BasePredicate
{
bool operator ()(int a)
{
return a == 2;
}
};
int Find2(list<int> a_list, BasePredicate &p)
{
list<int>::iterator iter = find_if(a_list.begin(), a_list.end(), p);
return (*iter);
}
int main (int argc, char *argv[])
{
// Example taken from
http://mrcadm1/silicongraphic/stl_doc/List.html
list<int> L;
L.push_back(0);
L.push_front(1);
L.insert(++L.begin(), 2);
DerivedPredicate my_predicate;
assert ( Find2( L, my_predicate ) == 2);
}
At first sight, it looks OK but the third parameter of the the STL
function find_if is passed by copy and the parameter being declared in
the header of Find2() is a pure virtual class. Therefore, the g++
compiler should report an error saying that an abstract class cannot be
instantiated. It doesn't. Instead, it compiles and links but crashes on
run time caused by a pure virtual function call.
Moreover, if the base class in the example above was not pure virtual,
the operator in the the derived class would never be called because the
predicate in STL find_if() is passed by copy and since it is
templatized, it creates THAT type of object, not the derived one. I
don't know if it's a bug in STL or if it's part of the standard but it's
a little ugly. The only way we found to fix the problem was to declare
Find2() as follow:
template <class P>
int Find2(list<int> a_list, P &p)
but as far as I understand, it would not be necessary if the predicate
was passed by reference.
--
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
Roger Leblanc Software Developer
General DataComm
Multimedia R&D Center