Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-11-27 21:34:30


----- Original Message -----
From: "Fernando Cacciola" <fcacciola_at_[hidden]>

> therefore, I ended up using:
>
> template<int N> struct int_t { BOOST_STATIC_CONSTANT(int, value = N ) ; }
;
> template<int N> struct int2type { typedef int_t<N> type ; } ;
>
> which is used as in:
>
> add_t< int2type<2>::type, int2type<3>::type >::type ;
>
> Is this any useful to the discussion?

Actually, yes, IMO. I think your formulation clarifies things a lot.
int_t might even be called int2int: it is a unary metafunction converting
ints to ints, while int2type is a unary metafunction converting ints to
types.

On the other hand, you can only use these as metafunction objects (to be
passed to meta-algorithms) if you're willing to use template template
parameters, which have a lot of drawbacks. A more flexible design would be,
e.g.:

struct int_to_int
{
    template <int N>
    struct apply
    {
        BOOST_STATIC_CONSTANT(int, value = N);
    };
};

struct int_to_type
{
    template <int N>
    struct apply
    {
        typedef int_to_int::apply<N> type;
    };
};

But then, wouldn't you combine them?

struct int_identity
{
    template <int N>
    struct apply
    {
        BOOST_STATIC_CONSTANT(int, value = N);
        typedef int type;
    };
};

-Dave


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