|
From: DU V. DE V. F. G. P. <fra...@ca...> - 2007-04-19 15:11:30
|
Fran=E7ois,
it doesn't sound right to me, for a couple of reasons:
>- you're changing the std::vector interface by adding operators. I'm =
not
>sure that it is legal C++. Moreover, this might be done without the
>user knowing it if he happens to include a file which in turn includes
>your new operators. Thus, code which according to the C++ standard is
>not valid (such as w =3D u+v if u,v,and w are std::vectors) would =
silently
>become legal. I'm not comfortable with this.
We could provide only specialized templates implementation. (for double, =
even if it some of them might also make sense with other data like =
Date,...) Anyway I'm pretty sure that any decent compiler would squeal =
if the corresponding elementwise operation doesn't exist.
>- there's a conceptual difference between the two classes, and using
>std::vector for both would confuse the issue. std::vector is a
>container; Array is the linear-algebra concept of an array. I, too, had
>been thinking of replacing Array with an STL class, but the right one
>would be std::valarray---which models the right concept and already
>defines the required operators.
I agree with you that a general purpose container and an Array are two =
different beasts. About the valarray Josuttis doesn't seem to be a great =
fan:
"At the time of this writing, no such implementation is known, and =
standard valarrays are, generally speaking, quite inefficient at =
performing the operations for which they were designed." (C++ templates: =
The complete guide)
Maybe they have improved it since ... :-)
Rgds,
Fran=E7ois
PS: here is an example of the operators I have defined:
inline const Disposable<std::vector<Real> > operator+(const =
std::vector<Real>& v1, const std::vector<Real>& v2) {
QL_REQUIRE(v1.size() =3D=3D v2.size(),
" vectors with different sizes (" << v1.size() << ", =
"
<< v2.size() << ") cannot be added");
std::vector<Real> result(v1.size());
std::transform(v1.begin(),v1.end(),v2.begin(),result.begin(),
std::plus<Real>());
return result;
}
PPS: I don't want to be pushy about this. It is just an idea I'm playing =
with.
|