Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-08-27 06:45:17


From: <helmut.zeisel_at_[hidden]>
> --- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> > template<class Tag, class A> struct extensible_traits;
> >
> > struct is_even_tag {};
> >
> > template<class Int> struct extensible_traits<is_even_tag, Int>
> > {
> > typedef bool result_type;
> > static result_type f(Int i) { return i % 2 == 0; }
> > };
> >
>
> Do I understand correctly:
>
> Implementation of is_even:
>
> template<typename T> bool is_even(const T& n)
> {
> return extensible_traits<is_even_tag,T>::f();
> };
>
> Usage:
>
> int n;
> ...
> if(is_even(n)) {...}

Yes, that's it.

> Why do you need result_type?

In this particular case I suppose that it's not needed since is_even will
always return bool. I only included it for illustration. Other operations
may have more flexible signatures and some sort of result_type support would
be necessary.

--
Peter Dimov
Multi Media Ltd.

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