Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2007-11-22 18:12:28


shunsuke wrote:
> Hi,
>
> (Now my question was simplified.)
> How can a sequence object be transformed into fusion::vector object, keeping mutability of elements?
>
>
> For example,
>
> When at_c return type Sequence of input is:
>
> int&, char, double const&, ...
>
> , a type Sequence I need is:
>
> fusion::vector<result_of<F(int&)>::type, result_of<F(char)>::type, result_of<F(double
> const&)>::type, ...>
>
>
> Note that the output I need is not a fusion::transform_view but a fusion::vector.
> as_vector doens't work, because as_vector makes use of value_of.
> Am I missing some predefined function?

I think you misunderstood some of my previous posts. value_of is only
used to compute the desired result. There is actually no corresponding
value_of function. It is only a metafunction, unlike /at/ which has
both a result_of metafunction and a function. When the input sequence
is actually traversed to generate the result, /at/ is the one being
called. Hence, the sequence above is what is actually generated by
as_vector.

So, if you are sure that you have real l-values, the transform function:

     struct f
     {
         template <typename FunCall>
         struct result;

         template <typename Fun>
         struct result<Fun(int&)>
         {
             typedef int& type;
         };

         template <typename Fun>
         struct result<Fun(int)>
         {
             typedef int& type;
         };

         int& operator()(int& i) const
         {
             return i;
         }
     };

is perfectly safe. In fact, it won't compile if you do not have an lvalue.
This won't compile if you give it an mpl::vector, for example (which
doesn't have lvalue elements) because your operator() specifically asks
for a reference. The second struct result<Fun(int)> is a pure-metafunction
call called by value_at. You can read it as: the type will be an int&
if the element type is an int -- it's a type only computation.

It's unfortunate and a bit unintuitive to be using the result_of scheme
to compute the value_at metafunction here. MPL uses the nested "apply"
for this purpose. OTOH, it would be annoying to have to use "apply"
for value_at and "result" for /at/. It's a compromise.

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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