Topic: std::complex and default initialization


Author: tarjeik@chemcon.no (Tarjei Knapstad)
Date: Wed, 22 Jan 2003 18:05:37 +0000 (UTC)
Raw View
I've got a query/proposal regarding the std::complex class.

The standard dictates that the real and imaginary values should have
default values, specifically:

complex(const T& re = T(), const T& im = T());

and for the specializations (float, double, long double) they are given
default values of 0.0.

I cannot see any good reason for doing this. std::complex is basically a
numeric type, so initializing
the real and imaginary parts to 0 is no more sensible than leaving them
uninitialized. Considering
I had made a matrix class able to use std::complex as it's type, default
constructing such a matrix
with dimensions say 1000 by 1000, would leave me with 2 million wasted
floating point assignments.
This may very well lead to noticeable and unnecessary performance hits
in compute intensive code.

My query/proposal: Why are there not two constructors for std::complex,
one default which does
no initialization of the real and imaginary parts, and one where you
explicitly initialize them yourself
leaving the imaginary part with a default value of 0 to allow
std::complex to be used as a regular
floating point value (inefficient and with the limitations of complex
numbers of course).
For the float specialization:

complex() {}
complex(float re, float im = 0.0f)

Regards,
--
Tarjei

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]