From: Edward Diener (eddielee_at_[hidden])
Date: 2003-07-11 05:57:24
Andy Sawyer wrote:
> From: Andy Sawyer <boost_at_[hidden]>
> To: Boost mailing list <boost_at_[hidden]>
> Subject: [boost] Re: functors for taking apart std::pair?
> Date: Thu, 10 Jul 2003 22:10:42 +0100
>>>>>> "ED" == Edward Diener <eddielee_at_[hidden]> writes:
>> Marshall Clow wrote:
> >> I recently had a need for a functor to return a component of a
> >> std::pair, and I was surprised to see that they didn't exist
> >> either in the standard library or in boost.
>> Matt Austern lists in his excellent book "Generic Programming
>> and the STL" the functors select1st and select2nd for getting a
>> pair's "first" and "second" members repsectively. His note to
>> both says that they were in the original HP STL and in the SGI
>> STL although they are not present in the current C++ Standard
> Marshall's "first" and "second" are slightly different to the HP
> template <class T1, class T2>
> struct first: std::unary_function< std::pair <T1, T2>, T1>
> template<typename Pair>
> struct select1st
> : std::unary_function< Pair, typename Pair::first_type>
Yes, I see it now. Marshall's version seems a little more redundant since
one has to specify both types while the HP version just has one specifying a
single pair type.
> The HP version has the advantage of working for "pair-like" pairs
> other than std::pair, and (IIRC) it returns a reference to (rather
> than a copy of) the selected member.
> FWIW, I've found both versions (or their moral equivalent) useful over
> the years.
> I think these disappeared from the standard around the same time as
> project1st/project2nd? (or at least, didn't make it in at around the
> same time :)
Yes, Austern's book also mentions project1st and project2nd.
I believe one can build an even more flexible function object which returns
an object of any type among multiple types using the Boost Tuple
implementation. Of course one would have to pass an index to such a function
object, which makes it a little more complicated to use than the pair
implementations being discussed. Or one could have tuple_select1st...
through tuple_selectxxx function objects up to some arbitrary limit. Maybe
Jaakko Jarvi can add such a function object to the Tuple library if others
find it useful.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk