This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: function object, function pointer and function.


Hi,

In the situation you presented, the & in your "int count1 = count_if(v1.begin(), v1.end(), &lessThan20Function);" line is superfluous. Although it is perfectly good code, and acceptable to all compilers.

In my experience, it would be omitted. By convention.

Also in my experience, there is a preference to function objects over function pointers.

For some really neat stuff, the BOOST (www.boost.org) library has a lambda library, which would allow you to put the body of the predicate right in your count_if statement -- and in a quite abbreviated form. It'd look something like...

int count = count_if(v1.begin(), v1.end(), _1 > 20);

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]