Just experimenting with multiple return values using this simple snippet that calculates sum and mean from a container.
template<typename Iter>
std::tuple<double, double> summean(Iter first1, Iter last1)
{
double sum = std::accumulate(first1, last1, 0.0);
double mean = sum / (last1-first1);
return {sum, mean};
}
The demo uses double precision calculation just as a demo. Is there an elegant way to use the precision of the values in the container in the calculations for the return types of the tuple?
You can use std::iterator_traits::value_type:
typedef typename std::iterator_traits<Iter>::value_type value_type;
As for the function declaration and definition, in old-school C++03 style you could do the following:
template<typename Iter>
std::pair<typename std::iterator_traits<Iter>::value_type,
typename std::iterator_traits<Iter>::value_type>
summean(Iter first1, Iter last1)
{
typedef typename std::iterator_traits<Iter>::value_type value_type;
value_type sum = std::accumulate(first1, last1, value_type());
value_type mean = sum / (last1-first1);
return std::make_pair(sum, mean);
}
And since C++11 you can use decltype and trailing return type to make it less verbose:
template<typename Iter>
auto summean(Iter first1, Iter last1)->decltype(std::make_tuple(*first1, *first))
{
using value_type = typename std::iterator_traits<Iter>::value_type;
value_type sum = std::accumulate(first1, last1, value_type());
value_type mean = sum / (last1-first1);
return make_tuple(sum, mean);
}
You probably want to return a std::pair of double instead of std::tuple. As for your problem you can use the value_type member taken from std::iterator_traits:
template<typename Iter>
auto
summean(Iter first1, Iter last1) -> decltype(std::make_pair(*first1, double{}))
{
using value_type = typename std::iterator_traits<Iter>::value_type;
double sum = std::accumulate(first1, last1, value_type{});
double mean = sum / (last1-first1);
return {sum, mean};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With