Is there a way in C ++ 11 to implement a function sqrtthat works for both positive and negative input values double? I would like the return type to std::complex<double>be if the input is negative, and a doubleif it is positive. I understand that the simple solution is to always return std::complex<double>, but this is not what I am looking for.
The following is an example of my first attempt, however this will not compile due to the presence ain the return type:
inline decltype((a > 0)?(double):(std::complex<double>)) sqrt(const double& a)
{
if(a > 0)
{
return std::sqrt(a);
}
else
{
return ((std::complex<double>(0.0,1.0))*std::sqrt(-a));
}
}
source
share