Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-09-26 16:56:17


From: "Jaakko Jarvi" <jajarvi_at_[hidden]>

> I'll give the issue another though. Could you send me an example
> that shows the benefits of the implementation where where cons<H,T>
> inherits from T?

The problem is this:

I'm given a tuple of up to three elements. At most one is an instance of
each of: a string literal, args<...>, or some other class type. I want a
way to sort out which is which. The implicit Derived->Base conversion
really helps in generating a function which operates on the right element.

I think the following would work (without partial specialization, of
course) if tuples had the inheritance model:

template <class Tuple> struct def_helper;

template <>
struct def_helper<tuples::null_type>
{
    static char const* docstring(tuples::null_type const&)
    {
        return 0;
    }

    static args<> args(tuples::null_type const&)
    {
        return args<>();
    }

    static default_call_policies policies(tuples::null_type const&)
    {
        return default_call_policies();
    }
}

template <bool is_class = false, bool is_args = false>
struct def_helper_impl
{
    template <class Tuple>
    struct apply
        : def_helper<typename Tuple::tail_type>
    {
        static char const* docstring(Tuple const& t)
        {
            return t.get_head();
        }
    };
};

template <>
struct def_helper_impl<true, true>
{
    template <class Tuple>
    struct apply
        : def_helper<typename Tuple::tail_type>
    {
        static typename Tuple::head_type args(Tuple const& t)
        {
            return t.get_head();
        }
    };
};

template <>
struct def_helper_impl<true, false>
{
    template <class Tuple>
    struct apply
        : def_helper<typename Tuple::tail_type>
    {
        static typename Tuple::head_type policies(Tuple const& t)
        {
            return t.get_head();
        }
    };
};

template <class Tuple>
struct def_helper
    : def_helper_impl<
        is_class<typename Tuple::head_type>:value
        , is_args<typename Tuple::head_type>:value
>::template apply<Tuple>
{
};

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


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