Specializing templates based on methods : <p>Recently I've been programming a lot in Java, now I'm coming back to my C++ roots (I really started missing the pointers and segmentation faults). Knowing that C++ has a broad support for templates I was wondering whether it has some capabilities of Java which could be useful for writing generalized code. Lets say I'have two groups of classes. One of them has the <code>first()</code> method, the other one has the <code>second()</code> method. Is there a way of specializing the templates to be picked by the compiler depending on the methods one class possesses? I'm aiming at behavior which is similar to the one of Java:</p>

<pre><code>public class Main {
    public static void main(String[] args) {
        First first = () -&gt; System.out.println("first");
        Second second = () -&gt; System.out.println("second");
        method(first);
        method(second);
    }

    static &lt;T extends First&gt; void method(T argument) {
        argument.first();   
    }

    static &lt;T extends Second&gt; void method(T argument) {
        argument.second();
    }
}
</code></pre>

<p>Where <code>First</code> and <code>Second</code> are interfaces. I know I could group both of these groups by deriving each of them from an upper class, but it's not always possible (no autoboxing in C++ and some classes don't inherit from a common ancestor).</p>

<p>A good example of my needs is the STL library, where some classes have methods like <code>push()</code> and some others have  <code>insert()</code> or <code>push_back()</code>. Lets say I want to create an function which has to insert multiple values into an container using an variadic function. In Java it's easy to perform because collections have a common ancestor. In C++ on the other hand it's not always the case. I tried it by duck-typing, but the compiler yields an error message:</p>

<pre><code>template &lt;typename T&gt;
void generic_fcn(T argument) {
    argument.first();
}

template &lt;typename T&gt;
void generic_fcn(T argument) {
    argument.second();
}
</code></pre>

<p>So my question is: Is implementing such behavior possible without creating  unnecessary boileplate code by specializing every single case?</p>
