Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-06-06 07:45:12


On Mon, Jun 06, 2005 at 12:22:14PM +0800, Manfred Doudar wrote:

> Dave Harris wrote:
>
> >In-Reply-To: <005501c566b3$1fc76eb0$2b792518_at_heronnest>
> >cdiggins_at_[hidden] (christopher diggins) wrote (abridged):
> >
> >>>I strongly disagree. Forcing initialization makes code that is not
> >>>useful for high performance numerical processing. When we declare a
> >>>container of 1000000 elements, we don't want to waste time
> >>>initializing each, unless we want to.
> >>>
> >>I think the best solution in this case is to use an alternative
> >>"collection" for numerical processing. Using a class intended as
> >>general purpose array for numerical processing I think would not be a
> >>good idea.
> >>
> >
> >I doubt we need a whole class. A special argument might do, to let us
> >choose:
> >
> > boost::array<int,1000> b( boost::uninitialised );
> > boost::array<int,1000> b( boost::initialised );
> >
>
> That's a reasonable proposition. But for the initialized case, you want
> to be specifying a value to initialize to - zero is *not* a reasonable
> default for all circumstance.

Besides which, you can already default-initialise (i.e. 0 for builtins)
an array of any size:

    boost::array<int,1000> b = { };

which is the equivalent of:

    std::vector<int> v(1000);

For those who would want to construct an array from a range of
iterators, construct it uninitialised and use std::generate().

If you want the array to be const then copy-construct it from the return
value of an inline function that uses std::generate() (or some other
method). Inlining and RVO should make that pretty efficient, and if
you're doing this it's probably once at startup anyway.

For those who want the equivalent of:

    vector::vector(size_type, const value_type&);

you could always use the preprocessor:

    boost::array<int,1000> b = {
        MAGIC_ARRAY_INIT(1000, 5)
    };

I think boost::array works fine uninitialised by default.

jon


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