Boost logo

Boost :

From: Victor A. Wagner, Jr. (vawjr_at_[hidden])
Date: 2002-07-25 03:22:12


only one small nit...see below
At Thursday 2002/07/25 00:38, you wrote:
>Maciej Sobczak wrote:
>>Some time ago I wrote the adaptor class that allows you to wrap
>>*arbitrary* functor (less, greater, etc.) in a uniform manner:
>
>Of course, forgot to mention: that adaptor was for different, specific
>purposes.
>The one for sorting (for operations on two aggregate objects) can look
>like here (with complete test program, stripped from #includes):
>
>template <class T, typename M, class OP>
>class oper_on_mem_t : binary_function<T, M, bool>
>{
>public:
> explicit oper_on_mem_t(M T:: *m, OP op) : m_(m), op_(op) {}
>
> bool operator()(const T &lhs, const T &rhs) const
> {
> return op_(lhs.*m_, rhs.*m_);
> }
>private:
> M T::* m_;
> OP op_;
>};
>
>template <class T, typename M, class OP>
>oper_on_mem_t<T, M, OP> oper_on_mem(M T:: *m, OP op)
>{
> return oper_on_mem_t<T, M, OP>(m, op);
>}
>
>struct S { int a, b, c; };
>
>int main ()
>{
> S s1 = {1,2,3};
> S s2 = {3,1,2};
> S s3 = {2,3,1};
>
> vector<S> v;
> v.push_back(s1);
> v.push_back(s2);
> v.push_back(s3);
>
> sort(v.begin(), v.end(), oper_on_mem(&S::a, less<int>()));

it's a shame you have to say <int> here.... it's deducible from S::a
then again...maybe there is a way... I'm by no means an expert on writing
templates

> for (vector<S>::iterator i = v.begin(); i != v.end(); ++i)
> cout << "(" << i->a << ", " << i->b
> << ", " << i->c << ")" << endl;
>
> return 0;
>}
>
>Prints:
>(1, 2, 3)
>(2, 3, 1)
>(3, 1, 2)
>
>The motivation is to write the adaptor that will be able to accommodate
>different needs, so that users will be able to use it uniformly with
>different, supplied operations. This allows also to write generic code
>that is independent of the actually provided comparison functor.
>
>Functors like less_element or greater_element cannot achieve this.
>
>Does Boost have something similar to this?
>
>Cheers,
>
>Maciej Sobczak
>http://www.maciejsobczak.com/
>
>_______________________________________________
>Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
               "There oughta be a law"


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk