Get the indices of a range elements by relative order of the values I've just asked

What's the idiomatic way to get the sort order of data, without actually applying it?

(or see the original it was found to be a dupe on). So, I want to implement properly what seems to be the only suggested option - generating an array of indices and sorting it by the original data's relative order. Here's my code (ignoring includes):

template&lt;typename RAIterator, typename Comparator, typename Size = size_t&gt;
inline std::vector&lt;Size&gt;
sorted_indices(RAIterator first, RAIterator last, Comparator comparator)
{
    std::vector&lt;Size&gt; result(last - first);
    std::iota(result.begin(), result.end(), 0);
    auto adapted_comparator = [&amp;first, comparator](Size lhs, Size rhs) {
        return comparator(first[lhs], first[rhs]);
    };
    std::sort(result.begin(), result.end(), adapted_comparator);
    return result;
}

template&lt;typename RAIterator, typename Size = size_t&gt;
inline std::vector&lt;Size&gt;
sorted_indices(RAIterator first, RAIterator last)
{
    using value_type = typename std::iterator_traits&lt;RAIterator&gt;::value_type;
    auto comparator = [](const value_type&amp; lhs, const value_type&amp; rhs) { return lhs &lt; rhs; };
    return sorted_indices&lt;RAIterator, decltype(comparator), Size&gt;(first, last, comparator);
}

template&lt;typename Container, typename Comparator, typename Size = typename Container::size_type&gt;
inline std::vector&lt;Size&gt; sorted_indices(Container&amp; container, Comparator comparator)
{
    return sorted_indices&lt;typename Container::const_iterator, Size&gt;(
        std::begin(container), std::end(container), comparator);
}

template&lt;typename Container, typename Size = typename Container::size_type&gt;
inline std::vector&lt;Size&gt; sorted_indices(Container&amp; container)
{
    return sorted_indices&lt;typename Container::const_iterator, Size&gt;(
        std::begin(container), std::end(container));
}


Have I covered all my bases or is there something I might be missing? Also, how legitimate is it to return the vector of indices?
Two thing I noticed I can do are:


use std::less&lt;T&gt; as a default comparator class
set a default value for the comparator parameter, using the default initializer of the comparator class


The combination of these two cuts code length by half without reducing legibility (!):

template&lt;
    typename RAIterator, 
    typename Size = size_t, 
    typename Compare = std::less&lt;typename std::iterator_traits&lt;RAIterator&gt;::value_type&gt;
&gt;
inline std::vector&lt;Size&gt;
sorted_indices(RAIterator first, RAIterator last, Compare compare = Compare())
{
    std::vector&lt;Size&gt; result(last - first);
    std::iota(result.begin(), result.end(), (Size) 0);
    auto lookup_and_compare = [&amp;first, compare](Size lhs, Size rhs) {
        return compare(first[lhs], first[rhs]);
    };
    std::sort(result.begin(), result.end(), lookup_and_compare);
    return result;
}

template&lt;
    typename Container,
    typename Size = typename Container::size_type,
    typename Compare = std::less&lt;typename Container::value_type&gt;
&gt;
inline std::vector&lt;Size&gt; sorted_indices(Container&amp; container, Compare compare = Compare())
{
    return sorted_indices&lt;typename Container::const_iterator, Size&gt;(
        std::begin(container), std::end(container), compare);
}


The downside is that you must then depend on &lt;iterator&gt; and &lt;functional&gt;; since I use them already for other things, I don't really mind.