Reputation: 771
For this template declaration:
template <class Iterator>
typename std::iterator_traits<Iterator>::value_type Mean(Iterator begin, Iterator end);
I would like to simplify the value_type typename with a "using declaration" or typedef. So that it is a easier to read the return type. Is there a way to do this in the declaration?
Upvotes: 0
Views: 100
Reputation: 1017
You can create an alias to std::iterator_traits<Iterator>::value_type
:
template <class It>
using itval= std::iterator_traits<It>::value_type;
//And use it:template <class Iterator>
itval<Iterator> Mean(Iterator begin, Iterator end);
In general for templates you should use the using
keyword. Typedef can't support templates.
Upvotes: 0
Reputation: 180710
You can add an template alias like
template <typename Iterator>
using iterator_value_type_t = typename std::iterator_traits<Iterator>::value_type;
and then you can use that with your function like
template <class Iterator>
iterator_value_type_t<Iterator> Mean(Iterator begin, Iterator end);
If you want to allow specifying a custom return type then you can move the return type into the template parameter list like
template <class Iterator,
class ReturnType = typename std::iterator_traits<Iterator>::value_type>
ReturnType Mean(Iterator begin, Iterator end);
Upvotes: 2