Boost logo

Boost Users :

Subject: Re: [Boost-users] [MPL] initialization of member that might be non-copyable
From: Larry Evans (cppljevans_at_[hidden])
Date: 2011-03-26 00:17:29


On 03/25/11 15:53, Noah Roberts wrote:
> On 3/25/2011 1:48 PM, Noah Roberts wrote:
> [snip bad code]
>
> I posted bits that I'd been experimenting and reverted stuff without
> testing. Here's corrected version:
>
>
> template < typename Field >
> struct field_value
> {
> typedef Field field;
> typedef typename Field::type ftype;
> ftype value;
>
> field_value(ftype const& t) : value(t) {}
> field_value() : value() {}
> };
>
> template < typename Field, typename Params, typename Enable = void >
> struct initializer
> {
> static typename Field::type apply(Params &)
> {
> return Field::type();
> }
> };
> template < typename Field, typename Params >
> struct initializer<Field,Params, typename boost::enable_if<
> boost::mpl::contains<typename Params::fields, Field> >::type >
> {
> static typename Field::type apply(Params & params)
> {
> return get<Field>(params);
> }
> };
>
> template < typename FieldValue, typename Params >
> FieldValue init_field(Params & pars)
> {
> return initializer<typename FieldValue::field, Params >::apply(pars);
> }
>
>
> template < typename FieldValue, typename More >
> struct record_base : FieldValue, More
> {
> template < typename Params >
> record_base(Params & pars)
> : FieldValue(init_field<FieldValue>(pars))
> , More(pars)
> {}
>
> record_base() : FieldValue(), More() {}
> };
> struct empty_record_base
> {
> template < typename Ignored >
> empty_record_base(Ignored&){}
> empty_record_base() {}
> };
>
> template < typename MplSequence >
> struct build_record
> {
> typedef boost::mpl::placeholders::_1 _1;
> typedef boost::mpl::placeholders::_2 _2;
>
> typedef typename boost::mpl::fold
> <
> MplSequence
> , empty_record_base
> , record_base< field_value<_2>, _1 >
> >::type type;
>
> // type should be record_base< field_value<field0>
> // , record_base< field_value<field1>
> // , ...
> // , record_base<field_value<fieldN>
> // , empty_record_base>
> // > ...
> //
> };

Hi Noah,

After comparing with Section 9.5 of the MPL book, it seems the
correspondence between this code and the book's is:

    this_code the book
    --------- ----------
    empty_record_base empty_base
    field_value wrap
    record_base inherit

and the major difference is is field_value, which, I suppose will be
used (after possibly some modification) to allow a non-copyable
member. Is that right? Since record_base has no constructor taking
this into account, I'm guessing you're really asking how to create
such a constructor and maybe modifications to field_value to make this
possible. Right?

-regards,
Larry

BTW, there's no get template defined above. I assume it will
be something like the get on p. 192 of the book?


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