Boost logo

Boost :

From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-05-16 02:09:57


----- Original Message -----
From: "Frank Mori Hess" <frank.hess_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, May 15, 2008 3:15 PM
Subject: [boost] [future] direct construct from value

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I've just noticed that neither Anthony's or Braddock's future libraries
> seem
> to support construction of a future directly from a value, like
>
> future<int> fut_int(5);
>
> Adding such a constructor gives support for implicit conversion of a value
> of
> type T to a future<T>. So a function which takes futures as arguments can
> also seamlessly accept values:
>
> future<void> my_func(future<int> f1, future<int> f2);
>
> my_func(fut_int, 3);
>
>
> This is important for the usability of poet::active_function, which takes
> all
> parameters as futures.

Hello,

you can do
    promise<int> p; future<int> f(p); p.set_value(3);
    my_func(fut_int, f);

which is much more cumberscome.

Where do you store the associated promise?
Maybe you need a wrapper of a promise and a future that behaves like that
that is convertible to a future.

template <typename T>
struct promise_future {
    promise_future(T& v) : p_(), f_(p_) {
        f_.set_value(v);
    }
    operator future<T>() { return f_;}
    promise<T> p_;
    future<T> f_;
}

The problem is that yoy have two user conversions, so you will need either
    promise_future<int> pf(3);
    my_func(fut_int, pf);
or
    my_func(fut_int, promise_future<int>(3));

Does this works for you?

Best
Vicente


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