If I have some code like:
using namespace std; namespace myNamespace { vector<float> sqrt( vector<float> v ) { return v; } void func() { vector<float> myVec = { 1, 2, 3, 4 }; std::cout << sqrt( myVec )[0] << std::endl; float myFloat = 4.0f; std::cout << sqrt( myFloat ) << std::endl;
then it will not compile unless I changed the highlighted line to use std::sqrt . What for? I understand that if I tried to override sqrt(float) in myNamespace , then I would have to qualify with std:: if I wanted to use the standard version of the library. The compiler seems to be trying to convert myFloat , and not just use the function in another ( std ) namespace.
One way to find this is to define sqrt(vector<float>) in the std , but this is not entirely correct and suppose the answer to this question is that overloading in std is illegal. This is probably not the way ...
How can I overload sqrt (or any other standard library cmath function, for that matter), so I donβt always have to choose which one to use and choose the compiler based on the passed parameters to the function?
Thanks.
source share