In This earlier posting, I had written that many things could go wrong, with the way C++ templates define complex numbers. Well, after studying the programming exercise which I was basing that posting on, I think I’ve finally found out, what the single problem in fact was.
In my programming exercise, I had defined the data-type ‘
complex<double_t>‘. This in itself caused a lot of problems, without being obvious to me as the culprit. The way C++ templates define complex numbers, will often derive the base-type of one complex number, from the base-type of another, preexisting one, just by transferring a template parameter. However, there are two situations where the templates, defined in the headers, can run into trouble with this:
- They can try to convert a ‘real number’ to a complex number, where the base-type of the derived complex number was never declared,
- They can try to mix mathematical operations between complex numbers and ‘real numbers’, in such a way that the type of the real number must match the base-type of the complex number exactly.
Specifically in situation (1) above, the templates will try the specializations of ‘
double‘, and ‘
long double‘, as educated guesses, for what type of complex number is required. And the problem may well be, that only these 3 template-specializations are attempted, not, ‘double_t’.
And in situation (2) above, I was not taking into consideration that my code was often providing literal numbers, that were of type ‘
double‘ by nature, not of type ‘
double_t‘. This created a mismatch.
In any case, now that I realize what my mistake was, as well as having removed all the ‘mixed computations, between complex and real numbers’, resulting in code that no longer generates errors, I am more confident that I only was days ago, in the C++ template-definitions, for complex numbers.
(Update 9/18/2019, 12h55 : )
An added observation would be, that when my code tried to find the absolute, of the imaginary component, of a complex number, that component was also a ‘
double_t‘ (deterministically), but the absolute function that’s predefined, again, only recognizes parameter-types ‘
double‘, and ‘
long double‘, in certain versions of the GCC compiler, which can again result in an incorrect match, with a working version of the absolute function.