Best way to choose the right function?

I am writing a utility program contains()and came up with this. My question is: is there a better way to choose the right function to handle the call?

template <class Container>
inline auto contains(Container const& c,
  typename Container::key_type const& key, int) noexcept(
    noexcept(c.end(), c.find(key))) ->
  decltype(c.find(key), true)
{
  return c.end() != c.find(key);
}

template <class Container>
inline auto contains(Container const& c,
  typename Container::value_type const& key, long) noexcept(
    noexcept(c.end(), ::std::find(c.begin(), c.end(), key))
)
{
  auto const cend(c.cend());

  return cend != ::std::find(c.cbegin(), cend, key);
}

template <class Container, typename T>
inline auto contains(Container const& c, T const& key) noexcept(
  noexcept(contains(c, key, 0))
)
{
  return contains(c, key, 0);
}
+4
source share
4 answers

For the record you can write:

#include "magic.h"

template <typename T, typename... Us>
using has_find = decltype(std::declval<T>().find(std::declval<Us>()...));

template <class Container, typename T>
auto contains(const Container& c, const T& key)
{
    return static_if<detect<has_find, decltype(c), decltype(key)>{}>
    (
        [&] (auto& cont) { return cont.end() != cont.find(key); },
        [&] (auto& cont) { return cont.end() != std::find(cont.begin(), cont.end(), key); }
    )(c);
}

where magic.hcontains:

#include <type_traits>

template <bool> struct tag {};

template <typename T, typename F>
auto static_if(tag<true>, T t, F f) { return t; }

template <typename T, typename F>
auto static_if(tag<false>, T t, F f) { return f; }

template <bool B, typename T, typename F>
auto static_if(T t, F f) { return static_if(tag<B>{}, t, f); }

template <bool B, typename T>
auto static_if(T t) { return static_if(tag<B>{}, t, [](auto&&...){}); }

template <typename...>
using void_t = void;

template <typename AlwaysVoid, template <typename...> class Operation, typename... Args>
struct detect_impl : std::false_type {};

template <template <typename...> class Operation, typename... Args>
struct detect_impl<void_t<Operation<Args...>>, Operation, Args...> : std::true_type {};

template <template <typename...> class Operation, typename... Args>
using detect = detect_impl<void, Operation, Args...>;

Demo

+5
source
namespace details {
  template<template<class...>class Z, class, class...Ts>
  struct can_apply:std::false_type{};
  template<template<class...>class Z, class...Ts>
  struct can_apply<Z,std::void_t<Z<Ts...>>,Ts...>:std::true_type{};
};
template<template<class...>class Z, class...Ts>
using can_apply=typename details::can_apply<Z,void,Ts...>::type;

this takes a pattern and arguments and tells you if you can apply it.

template<class T, class...Args>
using dot_find_r = decltype(std::declval<T>().find(std::declval<Args>()...));

template<class T, class...Args>
constexpr can_apply<dot_find_r, T, Args...> can_dot_find{};

Now we mark sending to myfind:

template<class C>
using iterator = decltype( ::std::begin(std::declval<C>()) );

namespace details {
  template<class Container, class Key>
  iterator<Container const&> myfind(
    std::false_type can_dot_find,
    Container const& c,
    Key const& key
  )
  noexcept(
    noexcept( ::std::find(::std::begin(c), ::std::end(c), key) )
  )
  {
    return ::std::find( ::std::begin(c), ::std::end(c), key );
  }

  template <class Container, class Key>
  iterator<Container const&> myfind(
    std::true_type can_dot_find,
    Container const& c,
    Key const& key
  ) noexcept(
    noexcept( c.find(key) )
  )
  {
    return c.find(key);
  }
}
template<class Container, class Key>
iterator<Container const&> myfind(
  Container const& c,
  Key const& k
) noexcept (
  details::myfind( can_dot_find<Container const&, Key const&>, c, k )
)
{
  return details::myfind( can_dot_find<Container const&, Key const&>, c, k );
}
template<class Container, class Key>
bool contains(
  Container const& c,
  Key const& k
) noexcept (
  noexcept( ::std::end(c), myfind( c, k ) )
)
{
  return myfind(c, k) != ::std::end(c);
}

As a bonus, the above version works with raw C-style arrays.

The next improvement I would make would be automatic ADL std::beginto make extensions beginin the case of dot_find.

std::optional<iterator> . " " , .

if (auto oit = search_for( container, key )) {
  // use *oit here as the iterator to the element, guaranteed not to be `end`
}

if (search_for( container, key )) {
  // key was there
}

, .

+5

, c.find, , std::find. , std::set.

( - ):

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <type_traits>
#include <set>
#include <cstdarg>

using namespace std;

template <typename T, typename Ret>
struct dummy {
    typedef Ret type;
};

template <class Container>
auto contains(const Container &c, typename Container::key_type const &key) ->
    typename dummy<decltype(c.find(key)), bool>::type {
    cout << "c.find" << endl;

    return c.end() != c.find(key);
}

template <class Container, typename ...T>
 typename std::enable_if<sizeof...(T)==1, bool>::type contains(const Container &c, const T&... args) {
    typename Container::value_type const &val = std::get<0>(std::tuple<const T&...>(args...));
    cout << "std::find" << endl;

    return c.cend() != find(c.cbegin(), c.cend(), val);
}

int main() {
    vector<int> v = {1,2,3};
    cout << contains(v,4) << contains(v,2) << endl;

    map<int, int> m;
    m[1] = 1;
    m[2] = 2;
    m[3] = 3;
    cout << contains(m,4) << contains(m,2) << endl;

    set<int> s;
    cout << contains(s,4) << contains(s,2) << endl;

    return 0;
}

:

  • contains, c.find(). , , .
  • , key_type value_type , std::find . 1.

, key_type , container.find OP, dummy:

template <class Container>
bool contains(const Container &c, typename Container::key_type const &key)
{
    return c.end() != c.find(key);
}

template <class Container, typename ...T>
 typename std::enable_if<sizeof...(T)==1, bool>::type contains(const Container &c, const T&... args) {
    typename Container::value_type const &val = std::get<0>(std::tuple<const T&...>(args...));
    return c.cend() != find(c.cbegin(), c.cend(), val);
}

, , , Container::find . . std::enable_if<! (Does Container have the find method?) , bool>::type, .

+2

since it findexists in associative containers, then you can explicitly make them as true types.

metafunctions:

template <class> struct has_find_impl:std::false_type{};
template <class T, class... Args> struct has_find_impl<std::set<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::map<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::multiset<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::multimap<T, Args...>>:std::true_type{};
template <class T> using has_find = has_find_impl<typename std::decay<T>::type>;

and use it like this:

template <class Container>
bool contains_impl(const Container& c, const typename Container::key_type& key, std::true_type)
{
    return c.find(key) != c.cend();
}

template <class Container>
 bool contains_impl(const Container& c, typename Container::const_reference key, std::false_type)
{
    return std::find(c.cbegin(), c.cend(), key) != c.cend();
}

template <class Container, class T>
bool contains(const Container& c, const T& key)
{
    return contains_impl(c, key, has_find<Container>{});
}

or use it with SFINAE. here is a complete example:

#include <iostream>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <vector>
#include <array>
#include <type_traits>


template <class> struct has_find_impl:std::false_type{};
template <class T, class... Args> struct has_find_impl<std::set<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::map<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::multiset<T, Args...>>:std::true_type{};
template <class T, class... Args> struct has_find_impl<std::multimap<T, Args...>>:std::true_type{};
template <class T> using has_find = has_find_impl<typename std::decay<T>::type>;


template <class Container>
typename std::enable_if<has_find<Container>::value, bool>::type
contains_impl(const Container& c, const typename Container::key_type& key)
{
    return c.find(key) != c.cend();
}

template <class Container>
typename std::enable_if<!has_find<Container>::value, bool>::type
contains_impl(const Container& c, typename Container::const_reference key)
{
    return std::find(c.cbegin(), c.cend(), key) != c.cend();
}

template <class Container, class T>
bool contains(const Container& c, const T& key)
{
    return contains_impl(c, key);
}

int main()
{
    std::cout << std::boolalpha;

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

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

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

    std::map<int, int> m = { { 1, 1}, { 2, 2}, { 3, 3} };
    std::cout << contains(m, 0) << "\n";
    std::cout << contains(m, 1) << "\n\n";
}
+1
source

All Articles