Insert into any sorted container The following is my attempt to implement a insert_sorted() C++ function that accepts any sorted STL container and item and inserts it at the right position in the container:
template&lt;template&lt;typename, typename, typename&gt; typename C, typename T1, typename Comp, typename A, typename T2&gt;
inline typename C&lt;std::enable_if_t&lt;std::is_convertible_v&lt;T1, std::remove_reference_t&lt;T2&gt;&gt;, T1&gt;, Comp, A&gt;::iterator
insert_sorted (C&lt;T1, Comp, A&gt; &amp; c, T2 &amp;&amp; item)
{
    return c.insert (c.end(), std::forward&lt;T2&gt; (item));
}

template&lt;template&lt;typename, typename&gt; typename C, typename T1, typename A, typename T2, typename Comp = std::less&lt;T1&gt;&gt;
inline typename C&lt;std::enable_if_t&lt;std::is_convertible_v&lt;T1, std::remove_reference_t&lt;T2&gt;&gt;, T1&gt;, A&gt;::iterator
insert_sorted (C&lt;T1, A&gt; &amp; c, T2 &amp;&amp; item, Comp comp = Comp{})
{
    return c.insert (std::upper_bound (std::begin (c), std::end (c), static_cast&lt;T1&gt;(item), comp), std::forward&lt;T2&gt; (item));
}


These two compile and work fine; the first overload works for set-like containers, while the second for vector-like containers.
I was wondering if you see any drawbacks to this solution and if there is a more generic way to approach the problem, that is, is it possible to write a single insert_sorted() function that does the same as the above two?
EDIT:
Thanks to the comments, I was able to replace the two template functions above with the following:
template&lt;typename C, typename T, typename Comp = std::less&lt;T&gt;, 
         typename = std::enable_if&lt;is_convertible_v&lt;std::remove_reference_t&lt;T&gt;, typename C::value_type&gt;&gt;&gt;
    inline auto insert_sorted (C &amp; c, T &amp;&amp; item, Comp comp = Comp{})
{
    return c.insert (std::upper_bound (std::begin (c), std::end (c), item, comp), std::forward&lt;T&gt; (item));
}

Thank you all for your help!
When we use std::begin() and std::end(), we should always consider whether we actually want to use begin() and end() of the type's namespace instead (argument-dependent lookup, aka ADL), with fallback to std:
using std::begin();
using std::end();
return c.insert(std::upper_bound(begin(c), end(c), item, comp),
                std::forward&lt;T&gt;(item));


When we're inserting to a sorted container such as std::set, we probably want to use the container's key_compare as default, rather than std::less&lt;T&gt;.  I suspect that will require splitting back into two functions again.