Simple Contains() Function I wrote simple generic function to check if any STL container has an element. It seems work fine. I have tested it with VC++, GCC and Clang compilers. It works perfectly.

How can I improve it further.

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;utility&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;vector&gt;
#include &lt;array&gt;
#include &lt;type_traits&gt;

// Detect if container has C::find
template &lt;typename C&gt; decltype(std::declval&lt;C&gt;().find(0), std::true_type{}) test(int);
template &lt;typename C&gt; std::false_type test(...);
template &lt;typename C&gt; using has_find = decltype(test&lt;C&gt;(0));

// Based on std::conditional
template &lt;bool B, typename T, typename F&gt;
struct conditional_impl
{
    constexpr static F pick(T&amp;&amp; , F&amp;&amp; f)
    {
        return std::forward&lt;F&gt;(f);
    }
};

template &lt;typename T, typename F&gt;
struct conditional_impl&lt;true, T, F&gt;
{
    constexpr static T pick(T&amp;&amp; t, F&amp;&amp;)
    {
        return std::forward&lt;T&gt;(t);
    }
};

template &lt;bool B, typename T, typename F&gt;
auto conditional(T&amp;&amp; t, F&amp;&amp; f)
{
    return conditional_impl&lt;B,T,F&gt;::pick(std::forward&lt;T&gt;(t), std::forward&lt;F&gt;(f));
}

// one for all and all for one
template &lt;typename C, typename T&gt;
auto contains(const C&amp; container, const T&amp; key)
{
    static auto first = [] (auto&amp;&amp; c, auto&amp;&amp; k)
    {
        return c.end() != c.find(k);
    };

    static auto second = [] (auto&amp;&amp; c, auto&amp;&amp; k)
    {
        return c.end() != std::find(c.begin(), c.end(), k);
    };

    auto op = conditional&lt;has_find&lt;C&gt;::value&gt;(first, second);

    return op(container, key);
}

int main()
{
    std::cout &lt;&lt; std::boolalpha;

    std::array&lt;int, 3&gt; a = {{ 1, 2, 3 }};
    std::cout &lt;&lt; contains(a, 0) &lt;&lt; "\n";
    std::cout &lt;&lt; contains(a, 1) &lt;&lt; "\n\n";

    std::vector&lt;int&gt; v = { 1, 2, 3 };
    std::cout &lt;&lt; contains(v, 0) &lt;&lt; "\n";
    std::cout &lt;&lt; contains(v, 1) &lt;&lt; "\n\n";

    std::set&lt;int&gt; s = { 1, 2, 3 };
    std::cout &lt;&lt; contains(s, 0) &lt;&lt; "\n";
    std::cout &lt;&lt; contains(s, 1) &lt;&lt; "\n\n";

    std::map&lt;int, int&gt; m = { { 1, 1}, { 2, 2}, { 3, 3} };
    std::cout &lt;&lt; contains(m, 0) &lt;&lt; "\n";
    std::cout &lt;&lt; contains(m, 1) &lt;&lt; "\n";
}
Nice toy! Took me a while to wrap my head around it. It looks ok. The only thing I was thinking about is why not to use std::enable_if as it is much simpler.

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;utility&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;vector&gt;
#include &lt;array&gt;
#include &lt;type_traits&gt;

// Detect if container has C::find
template &lt;typename C&gt; decltype(std::declval&lt;C&gt;().find(0), std::true_type{}) test(int);
template &lt;typename C&gt; std::false_type test(...);
template &lt;typename C&gt; using has_find = decltype(test&lt;C&gt;(0));

template &lt;typename C, typename T&gt;
typename std::enable_if&lt;has_find&lt;C&gt;::value, bool&gt;::type contains(const C&amp; container, const T&amp; key) {
    return c.end() != c.find(k);
}

template &lt;typename C, typename T&gt;
typename std::enable_if&lt;!has_find&lt;C&gt;::value, bool&gt;::type contains(const C&amp; container, const T&amp; key) {
    return c.end() != std::find(c.begin(), c.end(), k);
}

int main()
{
    /* */
}