Boost logo

Boost :

From: Christopher D. Russell (cdr_at_[hidden])
Date: 2004-12-09 08:55:19


Hello Hartmut, serendipitous question - I was working on this very problem
last night when I knocked off for the evening.

The gist of my approach is to return another variant from the binary version
of the static visitor that is bounded by a the permutations of the types of
the two input variants. My plan is to then operate on the return value using
the unary version of static visitor... Perhaps this isn't exactly what you
had in mind but it's all I could come up with. Perhaps others will jump in
and be able to suggest a better implementation?

// a set of bounded type classes
class A;
class B;
class C;
.
.
.
// a variant bounded by our bounded types
typedef boost::variant<A, B, C> vertex_variant_t;

// a class fully specialized for each ordered pair of bounded types
template <typename tail_vertex_T, typename head_vertex_T> class
edge_executor;

// a variant bounded explicitly the fully-specialized permutations of
A,B,C...N
// here: P(3,2) = 3!/(3-2)!=6
// Can anyone think of a clever way to succinctly write this typedef for
large P(n,r)?
typedef boost::variant< edge_executor<A,B>, edge_executor<A,C>,
edge_executor<B,C> edge_executor<B,A>, edge_executor<C,A>, edge_executor<C,B>
> edge_executor_variant_t;

class construct_edge_executor_variant :
    public boost::static_visitor<edge_executor_variant_t>
{
public:

    template <typename tail_vertex_T, typename head_vertex_T>
    edge_executor_variant_t operator() (tail_vertex_T const&, head_vertex_T
const&)
    {
        return edge_executor_variant_t( edge_executor<tail_vertex_T,
head_vertex_T> );
    }

    edge_executor_variant_t operator() (vertex_T const&)
    {
        return edge_executor_variant_t( edge_executor<vertex_T, vertex_T> );
    }
};

- HTH
Chris

"Hartmut Kaiser" <hartmutkaiser_at_[hidden]> wrote in message
news:1Cc3LI-0VxnTU0_at_afwd00.sul.t-online.com...
>
> Hi all,
>
> is there a means of using the binary visitation pattern provided by the
> Boost.Variant library and make the return type of the visitation process
> dependent on the types currently stored inside the visited variant object
> instances?
>
> I.e. I'm looking for some way to do the following:
>
> typedef boost::variant<unsigned long, long> variant_type;
>
> struct add_visitor : public ....
> {
> long operator()(long t1, long t2)
> {
> return t1 + t2;
> }
> template <typename T>
> unsigned long operator()(unsigned long t1, T t2)
> {
> return t1 + t2;
> }
> template <typename T>
> unsigned long operator()(T t1, unsigned long t2)
> {
> return t1 + t2;
> }
> };
>
> variant_type term1 = 1L;
> variant_type term2 = 2UL;
> variant_type term3 = -2L;
>
> variant_type unsigned_result =
> boost::apply_visitor(add_visitor(), term1, term2);
> variant_type signed_result =
> boost::apply_visitor(add_visitor(), term1, term3);
>
> Is this possible with the current implementation?
>
> Regards Hartmut
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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