I want to use the implementation of std::pow
(from cmath
) in some cases, and in other cases, I want to use myspace::pow
.
namespace myspace
{
template <typename T>
T pow(T val, unsigned exp)
{
if (exp == 0) return 1;
return val*pow(val,exp-1);
}
}
The different cases are determined by a template parameter.
template <typename T>
void myFunction()
{
auto val = pow(2.1,3);
/* ... */
}
If T == double
, I want val
to be calculated with std::pow
. If T == mydouble
, I want val
to be calculated with myspace::pow
. Now, I have a lot of lines like auto val = pow(2.1,3);
, and I would like to avoid to check for the type of T
for each line of code.
struct mydouble { /* ... */ };
myFunction<double>(); // uses std::pow
myFunction<mydouble>(); // uses myspace::pow
I've been breaking my head over this, but I can't find a solution. Any suggestions?