?
=?ISO-8859-1?Q?Christian_Brechb=FChler?=
The template std::valarray behaves pretty much like a mathematical
vector. Arithmetic operators apply elementwise. Now I'd like to extend
this to a user-defined type, e.g., complex.
Multiplying a double by a complex number gives a complex number. Now I
would like to multiply a valarray of doubles by a complex number and get
a valarray of complex numbers. Of course, a C++ compiler won't jump to
that conclusion; I need to define something to help it.
I didn't expect the following to compile:
#include <valarray>
#include <complex>
int main()
{
const double a0[] = {1.5, 4, 0};
std::valarray<double> a(a0, 3);
std::complex<double> z(1,2);
std::valarray<std::complex<double> > c (3);
c = z * a; // error: no match for operator *
return 0;
}
And indeed it gives "no match for `std::complex<double>& *
std::valarray<double>&' operator". (BTW, c is irrelevant to the
problem; I just want to show you what I expect.)
Now the question is, what would be the clean way to provide such
functionality? I want to avoid putting any notational burden on the
user. If possible, I'd like to avoid code duplication. Is there a
"generic" approach?
Thanks
Christian
vector. Arithmetic operators apply elementwise. Now I'd like to extend
this to a user-defined type, e.g., complex.
Multiplying a double by a complex number gives a complex number. Now I
would like to multiply a valarray of doubles by a complex number and get
a valarray of complex numbers. Of course, a C++ compiler won't jump to
that conclusion; I need to define something to help it.
I didn't expect the following to compile:
#include <valarray>
#include <complex>
int main()
{
const double a0[] = {1.5, 4, 0};
std::valarray<double> a(a0, 3);
std::complex<double> z(1,2);
std::valarray<std::complex<double> > c (3);
c = z * a; // error: no match for operator *
return 0;
}
And indeed it gives "no match for `std::complex<double>& *
std::valarray<double>&' operator". (BTW, c is irrelevant to the
problem; I just want to show you what I expect.)
Now the question is, what would be the clean way to provide such
functionality? I want to avoid putting any notational burden on the
user. If possible, I'd like to avoid code duplication. Is there a
"generic" approach?
Thanks
Christian