What is the advantage of inherit from unary_function template

A

Allerdyce.John

I would like to know what is the advantage of inherit from
unary_function template?

For example, I have this in my code:

What is the advantage of doing this:

class Same : std::unary_function<A*, bool>
{
public:
Same(A* a) : _a(a){};

bool operator()(A*a1);
private:
A* _a;
};


instead of this:

class Same
{
public:
Same(A* a) : _a(a){};

bool operator()(A*a1);
private:
A* _a;
};



typedef list<A*> AList
bool MyAList::contains(A* a) {
AList::iterator iter;

iter = find_if ( _aList.begin(), _aList.end(), Same(a));

if (iter == _aList.end())

return false;
else
return true;

}
 
R

roberts.noah

I would like to know what is the advantage of inherit from
unary_function template?

For example, I have this in my code:

What is the advantage of doing this:

class Same : std::unary_function<A*, bool>
{
public:
Same(A* a) : _a(a){};

bool operator()(A*a1);
private:
A* _a;
};

This allows you to use things like bind2nd and such. There are certain
requirements for "unary functions" and subclassing unary_function sets
all that up.
 
V

Victor Bazarov

I would like to know what is the advantage of inherit from
unary_function template?
[..]

At this point inheriting is done to get the typedefs 'unary_function'
declares (and 'binary_function' does that too).

V
 
V

Victor Bazarov

This allows you to use things like bind2nd and such. There are certain
requirements for "unary functions" and subclassing unary_function sets
all that up.

Do you think you can show an example where _not_ inheriting would make
it impossible to use 'bind2nd'? Especially in the case of an _unary_
function. Thanks!

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top