?
=?ISO-8859-1?Q?Christian_Brechb=FChler?=
Let me use complex numbers as a familiar example. The following is
taken verbatim from Bjarne Stroustrup, "The C++ Programming Language",
except the include line and the "no match" comments. It won't compile.
#include <complex>
// section 22.5:
// Throughout this book, I have used complex as a class rather than as a
// template. This is feasible because I assumed a bit of namespace magic to
// get the complex of double that I usually prefer:
typedef std::complex<double> complex;
// section 11.3:
// For example, from looking at a math textbook we would expect this to
work:
void f()
{
complex a = complex(1,2);
complex b = 3;
complex c = a+2.3;
complex d = 2+b; // no match for `int + complex&' operator
complex e = -b-c;
b = c*2*c; // no match for `complex& * int' operator
}
(Obviously we could kludge it by replacing 2 by 2.0 or double(2) etc.,
but I think we rightfully expect it to work exactly as Stroustrup wrote it.)
Questions: What was the rationale for implementing the standard library
like this?
Is there a generic way to accept any type that the usual arithmetic
conversions (C.6.3) can promote to double as the left or right argument
to a binary operator?
(I tried templating on the "simple" type, but when I do it for the left
and right argument, a+b becomes ambiguous. -- I there a way to express
that a template parameter "class T" must have a conversion to double?)
Thanks
Christian
taken verbatim from Bjarne Stroustrup, "The C++ Programming Language",
except the include line and the "no match" comments. It won't compile.
#include <complex>
// section 22.5:
// Throughout this book, I have used complex as a class rather than as a
// template. This is feasible because I assumed a bit of namespace magic to
// get the complex of double that I usually prefer:
typedef std::complex<double> complex;
// section 11.3:
// For example, from looking at a math textbook we would expect this to
work:
void f()
{
complex a = complex(1,2);
complex b = 3;
complex c = a+2.3;
complex d = 2+b; // no match for `int + complex&' operator
complex e = -b-c;
b = c*2*c; // no match for `complex& * int' operator
}
(Obviously we could kludge it by replacing 2 by 2.0 or double(2) etc.,
but I think we rightfully expect it to work exactly as Stroustrup wrote it.)
Questions: What was the rationale for implementing the standard library
like this?
Is there a generic way to accept any type that the usual arithmetic
conversions (C.6.3) can promote to double as the left or right argument
to a binary operator?
(I tried templating on the "simple" type, but when I do it for the left
and right argument, a+b becomes ambiguous. -- I there a way to express
that a template parameter "class T" must have a conversion to double?)
Thanks
Christian