Topic: Adaptors of <functional> - why wee needs them?


Author: jk@steel.orel.ru (Eugene Karpachov)
Date: Fri, 22 Jun 2001 17:54:10 GMT
Raw View
Thu, 21 Jun 2001 17:30:54 GMT Igor Krasnopeev =CE=C1=D0=C9=D3=C1=CC:
>
>So, why not allow to write just:
>
>for_each(V.begin(), V.end(), bind2nd(&Shape::Rotate, angle));
>for_each(Vp.begin(), Vp.end(), bind2nd(&Shape::Rotate, angle));
>
>and let the compiler to choose apropriate adaptor by itself?

It is design flaw, I think. It can be done, and AFAIR other binder
libraries allows it (using traits technique etc).

>Is there any else minuses, I can't see?

Me too.

--=20
jk

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: nickt@kipling.aus.deuba.com (Nick Thurn)
Date: Mon, 25 Jun 2001 17:13:29 GMT
Raw View
Eugene Karpachov wrote:
> Thu, 21 Jun 2001 17:30:54 GMT Igor Krasnopeev =CE=C1=D0=C9=D3=C1=CC:
> >
> >So, why not allow to write just:
> >
> >for_each(V.begin(), V.end(), bind2nd(&Shape::Rotate, angle));
> >for_each(Vp.begin(), Vp.end(), bind2nd(&Shape::Rotate, angle));
> >
> >and let the compiler to choose apropriate adaptor by itself?
>
> It is design flaw, I think. It can be done, and AFAIR other binder
> libraries allows it (using traits technique etc).
>
To be fair it may be because partial specialisation wasn't available
when the library was designed. (or am I talking through my hat here?).

Which other libraries, I'd be interested if could you point them out?
Do you mean boost? I dont recall adaptors with this functionality?

> >Is there any else minuses, I can't see?
>
> Me too.
>
One thing I have noticed in trying to implement this functionality
is that it works well for functions and member functions but gets
a bit messy when dealing with functors and hence propagates more
requirements back on the functor implementer (eg to derive from
binary_function or declare a return_type typedef).

I'm not sure this is a negative and frankly I think we need to
go as far as possible with simplifying the use of binders.

cheers
Nick

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Igor Krasnopeev <dark__raven@mail.ru>
Date: Thu, 21 Jun 2001 17:30:54 GMT
Raw View
Hello,

In <functional> header there is 3 function object types:

1. binders
2. negators
3. adaptors

While first and second do real job, third just tells the compiler
things it already knows:

vector<Shape> V;
vector<Shape*> Vp;

for_each(V.begin(), V.end(), bind2nd(mem_fun_ref(&Shape::Rotate), angle));
for_each(Vp.begin(), Vp.end(), bind2nd(mem_fun(&Shape::Rotate), angle));

In this example compiler knows, that &Shape::Rotate is member function,
type of  V elements is Shape (non pointer) and type of Vp elements -
Shape* (pointer).

So, why not allow to write just:

for_each(V.begin(), V.end(), bind2nd(&Shape::Rotate, angle));
for_each(Vp.begin(), Vp.end(), bind2nd(&Shape::Rotate, angle));

and let the compiler to choose apropriate adaptor by itself?

By using partial tempalte specialization this can be done for all
binders, adaptors and STL algorithms, without losing backward
compatibility.

+:

1. Shorter notation.
2. Less errors it code.

-:

1. A little more slow, that direct choicing, but it's not matter in 99.9% of cases.

Is there any else minuses, I can't see?

--
Igor Krasnopeev

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]