I have a function which creates an object and inserts it into a container. In most cases, the object type is the same as that of the container elements. Then, I don't want to have to specify the object type. But for containers holding std::variant, I want to be able to specify the object type as the first template parameter. Here's the non-working example:
template<typename T>
using remove_qualifiers = std::remove_cv_t<std::remove_reference_t<T>>;
template<typename T>
using IteratorType = decltype( std::declval<remove_qualifiers<T>>().end() );
template<typename T>
using ElementType = decltype( *( std::declval<remove_qualifiers<T>>().end() ) );
template<typename E = ElementType<T>, typename T, typename ... Args>
IteratorType<T> insert( T& someContainer, Args&& ... ){
IteratorType<T> it { someFindFunc() };
return someContainer.insert(it, E{std::forward<Args>(args) ...});
}
The problem is, that the default type for E is based on T, which at this point is not declared yet. But if I change the order, then calling the function becomes awkward.
How can I make this pattern work, so that I can call the function both with and without specifying E?
I guess this is likely a case of not finding the right search terms. I looked at this, this, this, and this one, and they don't appear to be what I'm looking for.
Consider wrap it into a class template so that you don't need to worry about specifying the type of the container.
Then,
Demo