|
Boost Users : |
Subject: Re: [Boost-users] Copy MPL sequence at runtime?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-03-18 10:06:50
AMDG
Rutger ter Borg wrote:
> Rhys Ulerich wrote:
>
>> I'd like to copy the contents of a boost::mpl::vector_c (or similar)
>> into an output iterator at runtime. I've not found a clean way to get
>> an MPL sequence into a runtime-usable form. So far, I've gotten the
>> included ugliness to work.
>>
>> 0) Is there some simple, idiomatic way to accomplish this goal that I've
>> missed?
>> 1) Any suggestions for how to clean this up? Specifically ways to
>> reduce the assumptions about the underlying MPL sequence or fixes for
>> braindead mistakes I've made?
>> 2) Is there some way to have syntax like 'seqcopy<S>(iter)' where the
>> iterator type is deduced but the sequence type is explicit?
>>
>
> Good morning,
>
> 0) the "only" run-time stuff in the MPL is mpl::for_each.
> 1) See attached, I guess this could be considered a bit cleaner.
> 2) Probably easiest by writing a function that does the type deduction for
> you.
>
How about this
#include <boost/mpl/int.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
using namespace boost;
using namespace boost::lambda;
int main() {
typedef boost::mpl::vector_c<int,2,4,6> values;
int output[3];
int* iter = &output[0];
mpl::for_each< values >( *var(iter)++ = _1 );
std::copy( &output[0], &output[3], std::ostream_iterator<int>(
std::cout, " " ) );
}
In Christ,
Steven Watanabe
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net