diff --git a/math/mathcore/inc/TComplex.h b/math/mathcore/inc/TComplex.h index 9aa26362c4c01..5bcadae570a6b 100644 --- a/math/mathcore/inc/TComplex.h +++ b/math/mathcore/inc/TComplex.h @@ -21,7 +21,9 @@ #include "TMath.h" #include "Rtypes.h" + #include +#include class TComplex { @@ -74,25 +76,32 @@ class TComplex { TComplex operator +() {return *this;} - // Simple operators complex - double - TComplex operator *(Double_t c) const - {return TComplex(fRe*c,fIm*c);} - TComplex operator +(Double_t c) const - {return TComplex(fRe+c, fIm);} - TComplex operator /(Double_t c) const - {return TComplex(fRe/c,fIm/c);} - TComplex operator -(Double_t c) const - {return TComplex(fRe-c, fIm);} - - // Simple operators double - complex - friend TComplex operator *(Double_t d, const TComplex & c) - {return TComplex(d*c.fRe,d*c.fIm);} - friend TComplex operator +(Double_t d, const TComplex & c) - {return TComplex(d+c.fRe, c.fIm);} - friend TComplex operator /(Double_t d, const TComplex & c) - {return TComplex(d*c.fRe,-d*c.fIm)/c.Rho2();} - friend TComplex operator -(Double_t d, const TComplex & c) - {return TComplex(d-c.fRe, -c.fIm);} + // Metafunction to figure out whether a type is arithmetic. This is used for + // the arithmetic operator implementation, so we can be sure that nothing + // unexpected will happen for non-arithmetic types that implement these + // operators with `double` themselves. + template + using enable_if_arithmetic = typename std::enable_if::value, bool>::type; + + // Simple operators complex - arithmetic + template = true> + TComplex operator *(T c) const { return {fRe*c,fIm*c}; } + template = true> + TComplex operator +(T c) const { return {fRe+c, fIm}; } + template = true> + TComplex operator /(T c) const { return {fRe/c,fIm/c}; } + template = true> + TComplex operator -(T c) const { return {fRe-c, fIm}; } + + // Simple operators arithmetic - complex + template = true> + friend TComplex operator *(T d, const TComplex & c) { return {d*c.fRe,d*c.fIm}; } + template = true> + friend TComplex operator +(T d, const TComplex & c) { return {d+c.fRe, c.fIm}; } + template = true> + friend TComplex operator /(T d, const TComplex & c) { return TComplex{d*c.fRe,-d*c.fIm} / c.Rho2(); } + template = true> + friend TComplex operator -(T d, const TComplex & c) { return {d-c.fRe, -c.fIm}; } // Convertors operator Double_t () const {return fRe;}