>> Is there a way to rename my squareroot to sqrt which calls the std libraries sqrt?
The problem is C++ doesn't allow you to to overload/specialize a function that differs only in return type from another. There are a couple of workarounds:
- Declare a set of pow functions in another namespace. It won't save you any typing, probably, but it's generally the best choice.
- Simply use a different name. Ok, so this isn't really a workaround, but it's effective. You just have to get used to it, that's all. For example, when I'm writing generic code, I never use the % operator since I can't be sure what the underlying type is. Instead, I've defined these:
Code:
template < typename Type >
Type mod( Type const& dividend, Type const& divisor, Type& remainder )
{
return remainder = dividend % divisor;
}
template < typename Type >
Type mod( Type const& dividend, Type const& divisor )
{
Type
remainder;
return mod( dividend, divisor, remainder );
}
float mod( float dividend, float divisor, float& remainder )
{
return remainder = fmod( dividend, divisor );
}
double mod( double dividend, double divisor, double& remainder )
{
return remainder = fmod( dividend, divisor );
}
int main( void )
{
cout << mod( 20, 3 ) << endl;
cout << mod( 20.0f, 3.5f ) << endl;
cout << mod( 20.0, 3.5 ) << endl;
}
Of course, if I'm defining a new class it doesn't matter if I choose to overload operator % or mod(A, B, C) - either works. It took a little getting used to, but now even if I'm using just plain ints I'll use mod instead of %, just out of habit.