If I have a little bit of 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; // need to use std::sqrt()
}
}
then it won't compile unless I changed the marked line to use std::sqrt
. Why? I understand that if I tried to redefine sqrt(float)
in myNamespace
then I'd have to qualify with std::
if I wanted the standard library version to be used. The compiler appears to try to convert myFloat
rather than just use a function in another (std
) namespace.
One way I found to get around this is to define sqrt(vector<float>)
in the std
namespace but that doesn't quite feel right and answers to this question suggest overloading in std
is illegal. Probably not the way to go then...
How can I overload sqrt
(or any other standard library cmath function, for that matter) so that I don't have to always qualify which one to use and have the compiler select based on the passed function parameters?
Thanks.