Can unary_function of STL of C++ (as implemented in gcc 4.7.x) have a virtual function?

130 views Asked by At

binder2nd is derived from unary_function.

The following code snippet does not work:

greater<int> g;

    //greater is derived from binary_function

//bind2nd returns binder2nd object and binder2nd is derived from unary_function

const unary_function<int,bool> &greater_than_0 = bind2nd(g, 0);

    // base class reference type holding reference to derived class object

remove_if(lst.begin(), lst.end(), greater_than_0);
    //does not work

Whereas the following code snippet works:

greater<int> g;

const binder2nd< greater<int> > &greater_than_0 = bind2nd(g, 0);

remove_if(lst.begin(), lst.end(), greater_than_0);

Will it be helpful to have a dummy virtual function in unary_function so that the first code snippet works. Can that improve the usage of STL?

1

There are 1 answers

1
Pete Becker On

unary_function is a convenience type that adds a couple of typedefs to your derived class. It is not polymorphic; if you write code that mentions it by name other than as a base class in a class definition you've almost certainly made a mistake.