Bug #949
complex(double real) should be marked as explicit
0%
Description
Related to 948 - now compiler is trying to call
sqrt(Complex z)
even when I do
double x(sqrt(16.));
because C++ has this weird, broken behaviour where it automatically calls constructors to do type conversions in function headers. Use explicit keyword to stop this please (it's in the style guide).
Updated by Rogers, Chris over 11 years ago
Humm, not sure what its doing - but to call sqrt() I needed to comment out both:
MAUS::Complex MAUS::sqrt(const MAUS::Complex& x) MAUS::Complex MAUS::sqrt(const double x)
Updated by Lane, Peter over 11 years ago
IIRC, because there are matches for sqrt() in the namespace you are in, the compiler stops looking for alternatives. If the <cmath> functions were indeed in the STL library then they would be defined within the std namespace and this wouldn't be a problem. But these functions are in fact throwbacks to C++'s C origins, so they are in the default namespace. You have to explicitly use "::sqrt(x)".
I am fine with removing the "const double x" function (admittedly this was probably a bad idea in the first place), but basically all of the math functions operating on MAUS::Complex arguments will conflict with <cmath> functions. I could rename them all, but I find this distasteful given that it defeats the whole purpose of using namespaces.
Another option might be to make MAUS::Complex a class instead of a typedef of gsl_complex, but that would complicate it's usage elsewhere. I'll have to think about this.
Updated by Lane, Peter over 11 years ago
I meant to say that I could make MAUS::Complex a class and make these functions members of that class. It kind of goes against the convention in C++ of using free functions if you can do the operation with the public class interface, but I'm not going to loose sleep over that.
Updated by Rogers, Chris over 11 years ago
I didn't realised that you could force back to the global namespace like that. I tend to like to group things into a subnamespace (or if they need some global data to work, maybe a class with everything declared static). I didn't realise the workaround though, so maybe it's a non-issue.
Updated by Rogers, Chris over 11 years ago
- Status changed from Open to Rejected
Humm, I'll reject these and just note that folks have to use ::sqrt rather than sqrt...