I want to ask is it possible to write overloaded function for arithmetic's type which returns double and for array-type which returns valarray. Simple example
template<typename T>
double foo(T x) {
return x;
}
template<typename T>
std::valarray<double> foo(T const &arr) {
std::valarray<double> valarr(1,1);
return valarr;
}
In result I get expected message: call to 'foo' is ambiguous. Is there any possibility to write such a functions?
I would really appreciate all comments.
CodePudding user response:
For C-array types, it would be:
template<typename T, std::size_t N>
std::valarray<double> foo(T const (&arr)[N])
{
// ...
}
CodePudding user response:
You can use C 20 concepts:
#include <valarray>
#include <type_traits>
template<typename T>
requires std::is_arithmetic_v<T>
double foo(T x) {
return x;
}
template<typename T>
requires std::ranges::random_access_range<T>
std::valarray<double>
foo(T const& arr) {
std::valarray<double> valarr(1,1);
return valarr;
}