|
Ublas : |
Subject: Re: [ublas] Why is there no constructor taking an initial value?
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-01-26 14:54:26
Karl Meerbergen skrev:
> Thorsten Ottosen wrote:
>>> It is not because STL containers have such constructors that other
>>> packages should follow the same ideas.
>>
>> It's one of boost's hallmarks, that we try to make as much as possible
>> similar to the STL. It's much easier to learn new libraries that way.
>
> Fine. Following your reasoning, we should have functions such as
> norm_2( v.begin(), v.end() )
> dot (v.begin(), v.end(), w.begin() )
> etc...
>
>
> This would be even more consistent with STL.
>
And what is wrong with that?
Anyway, I was referring to to the containers of the STL.
>> A constructor with three aguments is pretty clear IMO.
>>
>> For vectors, it would of course be a constructor with two arguments.
>>
>
> Would you add an additional argument to the constructors of
> vector_range, matrix_range etc... ?
I don't know those classes, so I can't comment.
> Finally:
>
> I do not think
> vector<double> v( 5, 0 );
> is more elegant (or easier to understand) than
>
> vector<double> v(5) ;
> fill( v, 0 ) ;
>
> Personally, I do not object strongly against such constructors, but I
> think it only adds confusion.
Well, C++ is an object oriented language, and in those people are very
much used to the idea that a constructor is in charge of initializing
the object. That's pretty basic. And I think you need a very good reason
for not to follow that.
Double construction is not something to strive for, and it can be
problematic because it makes it harder to reason about exception-safety
when you don't know if an object is initialized.
The current syntax is way to verboose for it to fit in one 80 char line:
ublas::triangular_matrix<Double> matrix( N, N );
matrix.assign( ublas::zero_matrix<Double>( N, N ) );
So I'll iterate my point: it's not about what you can and cannot do.
It's about how simple it is to initialize a object, and how easy it is
for users to learn how to do that. Even though I'm an experienced
developer, I had to look all over the documentation to figure out how to
initialize the matrix ... it used a lot of my time, and I'm sure it will
use a lot of time for the next person that starts to use the library.
-Thorsten