473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What is the advantage of inherit from unary_function template

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;

}

Jan 26 '06 #1
3 2195

Al************@gmail.com wrote:
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.

Jan 26 '06 #2
Al************@gmail.com wrote:
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
Jan 26 '06 #3
ro**********@gmail.com wrote:
Al************@gmail.com wrote:
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.


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
Jan 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: kyo | last post by:
class A { public: int a; }; struct testfun1 : public unary_function<const A&, bool> { bool operator()(const A &) const {
0
by: Jill Graham | last post by:
Hi folks, The pages of my website are built dynamically and are based on templates. A template can look like this : <table> <tr><td>This is the page header</td></tr> <tr><td>This is the...
6
by: blah | last post by:
Hi, Is it possible to force the typename of a template to inherit from a particular class? For example, something like this (conceptual; this doesn't compile): class Parent { public: ...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
4
by: yuyang08 | last post by:
Hello, everyone, I am wondering what is the hash function that is used in hash_map/hash_set. Can I replace it with my own hash function? Any comments on this? Thanks! -Andy
8
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
7
by: curious2007 | last post by:
Hello, I have the following code, but does not compile: #include <iostream> using namespace std; template<class Arg, class Result> struct unary_function {
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
6
by: jabbah | last post by:
Hi, im failing with my first try to use unary_function, i got something (which i partly borrowed from another forum): #include <algorithm> #include <functional> #include <iostream> using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.