Boost logo

Ublas :

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2006-01-03 11:47:12


Vardan Akopian wrote:

> On 12/28/05, Dima Sorkin <dsorkin_at_[hidden]> wrote:
>> Quoting "Harris, Chris GOSS EXT" :
>> > Maybe matrix should behave like std::vector and always default
>> > initialize its values.
>> Hi.
>> I don't understand the difference...
>> I always thought that in C++ "uninitialized" and
>> "default constructed" is the same, as ( at high level )
>> for each "unitialized" object default constructor is called anyway,
>> and then the call is simply optimized out if possible.
>
> That's only true for non-POD's, i.e. primitives such as int and double
> are never default initialized in C++. So for matrix<double> the
> internal array of double's is not default initialized. In STL though,
> it's part of the std::vector concept, that the internal array is
> initialized.
> I agree with Chris Harris: it looks like as of now there is no way to
> resize a matrix preserving the existing elements, but not initializing
> the new ones.
> However I think ublas made the correct choice of not default
> initializing vector and matrix values in constructors. In many
> situations, in numerical algorithms you need to fully populate the
> matrix in your code, so having the (potentially big) matrix default
> initialized would be a waste.
> I think, one solution could be to add another constructor and resize
> function, e.g.
> matrx::matrix(size_type size1, size_type size2, value_type init_value);
> matrix::resize(size_type size1, size_type size2, bool preserve,
> value_type init_value);
>
> But for now, if you need to 0-initialize the container, you'll need to
> call clear() after construction.
>
> -Vardan
>

I have patched ublas so that it does not default construct. This was done
in storage.hpp.

Then I added a constructor in vector.hpp

+ vector (size_type size, const value_type &init):
+ vector_container<self_type> (),
+ data_ (size, init) {}

So the user can choose. If you want to initialize, you can but but default
you don't.