I create a function with the same name as in std, why when I explicitly call the function std::pow, my pow is called? I tried to make it in Clion, Xcode on Mac, also Rider on windows I got "13" two times(with default compilers), but when I asked my fried to run this in Visual Studio, he got two times std::pow call. Im not using "using namespace std;"
#include <cmath>
double pow(double num, double exponent)
{
return 13.0;
}
int main()
{
double a = pow(10, 5);
double b = std::pow(10, 5);
return 0;
}
The function with name pow
and signature
pow(double, double)
appears in the standard library as a function with external linkage inherited from the C standard library. (See cppreference.)
Function signatures with these properties are reserved in the global namespace scope. A program that declares or defines a function with the same name and signature in the global namespace scope has undefined behavior. See [extern.names]/4 together with [reserved.names.general]/2.
So the question of calling the function doesn't even matter. The program already is invalid for defining that function. A different name must be chosen.
Or better, everything that you define yourself should go into a namespace scope that clearly belongs to yourself. Then you can explicitly qualify your calls with the name of your namespace scope and you won't have to take care of all of the special rules that apply to the global namespace scope, such as the one I explained here.