Topic: possible addition to <functional>
Author: Ronald Landheer-Cieslak <blytkerchan@gmail.com>
Date: Tue, 9 Oct 2007 08:58:07 CST Raw View
On Oct 2, 2:02 pm, int...@gmail.com wrote:
> On Oct 1, 10:49 pm, Ronald Landheer-Cieslak <blytkerc...@gmail.com>
> wrote:
>
> > I was just wondering whether there's any change of an apply_to_first,
> > an apply_to_second and an apply_to_nth function being added to
> > <functional>. The general idea would be to help in a case where you
> > have a range of tuples (or pairs) for which you want to apply an
> > existing function to a member in the tuple, without having to write
> > the loop by hand.
>
> You don't have to write the loop by hand - you have to write a functor
> presently, and in C++09 you would be able to use a local class for
> that. IMO, there's no point to provide specialized functions for
> anything more complicated that what std::bind already covers - there
> are just too many cases to cover. And if we get lambdas, they will
> provide a terse syntax for functors as well, even more so than what is
> proposed here.
The point is not to be terse and IMHO shouldn't be: the point is to be
clear. I see language (whether it by natural or artificial) as a way
of expressing one-self and programming languages in particular as a
way of expressing ones intent in such a way that both the machine
(which will have to execute and perform the intended actions) and
another programmer can understand. Working with tuples is a
commonplace thing to do and, IMO, needs a clearer way to allow the
application of some kind of function to a member of the tuple than a
lambda expression which, IMHO, tend to be far from clear.
This is a library issue, and the real question is whether there would
be a problem with complementary tools being part of the library. The
argument you're making (lambda expressions, local functors and bind
can all do it) is valid if complementary tools have no place in the
library. I'd say that they do (just look at the containers and how
many complementary tools there are there) and that the one I happen to
propose would make a logical addition to the toolbox we have, which
would fall nicely in a gap where lambda expressions, local functors
and bind do not allow a programmer to easily and eloquently express
himself.
rlc
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: Ronald Landheer-Cieslak <blytkerchan@gmail.com>
Date: Mon, 1 Oct 2007 12:49:24 CST Raw View
Hello everyone,
I was just wondering whether there's any change of an apply_to_first,
an apply_to_second and an apply_to_nth function being added to
<functional>. The general idea would be to help in a case where you
have a range of tuples (or pairs) for which you want to apply an
existing function to a member in the tuple, without having to write
the loop by hand.
For example: I have a vector:
typedef vector< pair< int, NotificationCallback > > Callbacks_;
Callbacks_ callbacks_;
in which I want to find an entry where the first of the pair is a
given value. The function I propose would allow me to do this:
Callbacks_::iterator where(find_if(callbacks_.begin(),
callbacks_.end(),
apply_to_first< Callbacks_::value_type >(bind2nd(equal_to< int >(),
the_value_I_want))));
The function itself would look a bit like this:
template < typename Pair, typename OpType >
struct apply_to_1st_impl_
{
typedef typename OpType::result_type result_type;
apply_to_1st_impl_(OpType op)
: op_(op)
{ /* no-op */ }
result_type operator()(const Pair & pair) const
{
return op_(pair.first);
}
OpType op_;
};
template < typename Pair, typename OpType >
apply_to_1st_impl_< Pair, OpType > apply_to_first(OpType op)
{
return apply_to_1st_impl_< Pair, OpType >(op);
}
with two more versions for the second and nth (nth taking a tuple
rather than a pair).
The idea of using a helper function is to not have to specify the type
of the operator to apply (OpType) - a bit like bind2nd..
If there is any interest for this, I'd be happy to write a more formal
proposal.
Regards,
Ronald Landheer-Cieslak
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: int19h@gmail.com
Date: Tue, 2 Oct 2007 12:02:00 CST Raw View
On Oct 1, 10:49 pm, Ronald Landheer-Cieslak <blytkerc...@gmail.com>
wrote:
> I was just wondering whether there's any change of an apply_to_first,
> an apply_to_second and an apply_to_nth function being added to
> <functional>. The general idea would be to help in a case where you
> have a range of tuples (or pairs) for which you want to apply an
> existing function to a member in the tuple, without having to write
> the loop by hand.
You don't have to write the loop by hand - you have to write a functor
presently, and in C++09 you would be able to use a local class for
that. IMO, there's no point to provide specialized functions for
anything more complicated that what std::bind already covers - there
are just too many cases to cover. And if we get lambdas, they will
provide a terse syntax for functors as well, even more so than what is
proposed here.
---
[ 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.comeaucomputing.com/csc/faq.html ]