简体   繁体   English

可以使用C ++的STL的unary_function(在gcc 4.7.x中实现)具有虚函数吗?

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

binder2nd is derived from unary_function . binder2nd派生自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. unary_function使用虚拟虚函数是否有帮助,以便第一个代码片段起作用。 Can that improve the usage of STL? 这可以改善STL的使用吗?

unary_function is a convenience type that adds a couple of typedefs to your derived class. unary_function是一种便利类型,它为您的派生类添加了几个typedef。 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. 如果您编写的代码除了作为类定义中的基类之外,通过名称提及它几乎肯定会犯错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM