|
Boost : |
From: Andy Sawyer (andys_at_[hidden])
Date: 2003-07-10 20:23:10
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:
ED> 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.
ED> Matt Austern lists in his excellent book "Generic Programming
ED> and the STL" the functors select1st and select2nd for getting a
ED> pair's "first" and "second" members repsectively. His note to
ED> both says that they were in the original HP STL and in the SGI
ED> STL although they are not present in the current C++ Standard
ED> Library.
Marshall's "first" and "second" are slightly different to the HP
versions:
template <class T1, class T2>
struct first: std::unary_function< std::pair <T1, T2>, T1>
...
vs.
template<typename Pair>
struct select1st
: std::unary_function< Pair, typename Pair::first_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 :)
Regards,
Andy S.
(/me disappears below the boost rader for a couple more years ;))
-- "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels it finds the darkness has always got there first, and is waiting for it." -- Terry Pratchett, Reaper Man
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk