 <p>I believe you want something like this</p> <pre><code>//class hierarchy to set the priority for type matching struct second_priority { }; struct first_priority : public second_priority {}; template&lt;typename T&gt; auto size_impl(T const &amp; data, second_priority t) -&gt; int { return sizeof(data); } template&lt;typename T&gt; auto size_impl(T const &amp; data , first_priority t) -&gt; decltype(data.size(),int()) { return data.size(); } template&lt;typename T&gt; int size(T const &amp; data ) { return size_impl(data,first_priority{}); } </code></pre>