Boost logo

Boost :

Subject: Re: [boost] [container] varray aka static_vector
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2013-02-11 16:46:39


Krzysztof Czainski wrote:
> 2013/2/11 Andrew Hundt <athundt_at_[hidden]>
>> Generally, built-in types are initialized when the constructor is
>> called explicitly. For example:
>>
>> int a; // a has the value of underlying memory
>> int b = int(); // b has a value of 0
>>
>> Right now the default behavior of our container is more like a vector
>> than an array when creating values using the default ctor. Default
>> constructable values are explicitly initialized in the varray ctor,
>> resize(), emplace() and emplace_back() functions, even when they are
>> trivially constructable. We find this behavior more intuitive and
>> therefore we have made it the default. If the performance needs to be
>> improved slightly the behavior may be changed or disabled in
>> container_detail::varray_traits<V, C, S>.
>>
>
> I assume I can't change the Strategy or emplace_back()/resize() behavior
> for just one particular varray object, can I?
>
> I mean:
>
> varray<int,5> a;
> varray<int,5> b;
>
> I'd like to change the Strategy only for b, can I do that?

You may do it by partially specialize varray_traits for your strategy.
Currently the varray allowing to pass user-defined strategy is
implemented in the container_detail namespace. So it would look more or
less like this:

namespace bc = boost::container;

// define my strategy
template <typename V>
struct my_strategy : public bc::container_detail::strategy::def<V> {};

// specialize traits for my strategy
namespace boost { namespace container { namespace container_detail {

template <typename V, typename C>
struct varray_traits<V, C, my_strategy>
   : public varray_traits<V, C, strategy::def<V> >
{
   typedef boost::true_type disable_trivial_init;
};

}}}

/*...*/

// use non-default version of varray
bc::container_detail::varray<int, 5, my_strategy<int> > b;

Future alternative:

Currently settings like disable_trivial_init are hidden deeply inside
details. However they might be a part of default Strategy concept.
Assuming that varray was moved to the boost::container namespace the
example would be shorter:

// define my strategy
template <typename V>
struct my_strategy : public bc::strategy::def<V>
{
   typedef boost::true_type disable_trivial_init;
};

/*...*/

// use non-default version of varray
bc::varray<int, 5, my_strategy<int> > b;

Regards,
Adam


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