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-04-04 15:30:41

On 04/03/11 13:59, Larry Evans wrote:
> 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:
> [snip]
> Hi Noah,
> Are you having any luck with this? I'd liks to see how you've
> done it and/or what difficulties you had doing it.
> TIA.
> -Larry

The code here:

has non-copyable template classes:

  < unsigned I
: charvec_u<I>
    double d;
    : d(-1000.0-double(I))
    charvec_double(char c)
    : charvec_u<I>(c)
    , d(-100.0-double(I))
    charvec_double(double _d)
    : d(_d)
    charvec_double(char c,double _d)
    : charvec_u<I>(c)
    , d(_d)
    charvec_double(charvec_double const&)
     * Disallow copy CTOR

used as components in a heterogeneous container type:

          < mpl::integral_c<index_numerals,index_0>
          , charvec_double<0>
          , charvec_double<1>
          , charvec_double<2>
          , charvec_double<3>

and initialized, without using the copy CTOR's. Instead,
it uses something like the named parameters syntax:

where, in this case, the name for the parameters is their
index template:


In the test code mentioned, this appears as:

          < index_numerals
          , index_0
          < index_numerals
          , index_3
        ( ndx3=double(1000.3) //show out-of-order initialization
        , ndx0='x'
      #if 1
        , index<index_numerals,index_2>() = 'z'
        //This should produce compile-time error because of multiple
index_0 associations.
        , ndx0='z'

Note the #else comments. If the same name is used more than once,
a compile error (obscure, as usual) is produced. The output
produced from the following print statements are:


Of course, in real code, the index_numerals would have more meaningful
names. The heart of the solution is in:

in templated CTOR:

      < index_type... Keys
      , typename... CtorTypes
      ( index_component
          < index_type
          , Keys
          , CtorTypes

Here, the Keys... are the indices, and the CtorTypes... are
the types of arguments to the components associated with the
corresonding key in Keys.... The value of those componens
are, obvious, retrieved using the my_comp member variable of
the index_component<Key,CtorType> values.



Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at