Home > front end >  How to implement a universal function for both sequence and associative container?
How to implement a universal function for both sequence and associative container?

Time:09-30

I am thinking to write a function that works both for sequence and associative container. That's something like

 template<class C, class V = typename C::key_type>
 bool has_val(const C& c, const V& v)`

Inside the function, I would like to

  1. Check if class C has member function const_iterator find(key_type) const for container classes like set/map.
  2. If it doesn't contain find(), then we use std::find() for sequence container like std::vector.

What's the best practice to check, (1)?

If what I described above is not the best, please advise if there's a better approach?

(Unfortunately I don't have access to newer Folly with macro FOLLY_create_member_invoker but I do have FOLLY_CREATE_HAS_MEMBER_FN_TRAITS. I couldn't successfully get it through, though)

CodePudding user response:

I am thinking to write a function that works both for sequence and associative container.

If you know which container/ specialization to the container you pass in to the function, then in it should be really obvious.

We can find this by following is_specialization_of:

template <typename Type, template <typename...> class Container>
inline constexpr bool is_specialization_of = false;

template <template <typename...> class Container, typename... Args>
inline constexpr bool is_specialization_of<Container<Args...>, Container> = true;

As next, you can make use of if constexpr, (Since ) and can do:

template<typename Container>
auto find_in_sequence_containers(const Container& container
    , typename Container::value_type const& val)
{
    return std::find(std::cbegin(container), std::cend(container), val);
}

template<typename Container>
auto find_in_associative_containers(const Container& container
    ,  typename Container::key_type const& key)
{
    return container.find(key);
}

template<typename Container, typename T2>
auto generic_find(const Container& container, T2 const& arg)
{
    if constexpr (is_specialization_of<Container, std::vector>)
    {
        return find_in_sequence_containers(container, arg);
    }
    if constexpr (is_specialization_of<Container, std::map>)
    {
        return find_in_associative_containers(container, arg);
    }
    // ... so on
}

By this way, you can avoid checking the member function availability(i.e. find), on the passed container.

(See an example code Online)

CodePudding user response:

Use SFINAE to detect whether to use c.find():

#include <algorithm>

template <class C, class V, typename = void>
constexpr inline bool use_mem_find = false;

template <class C, class V>
constexpr inline bool use_mem_find<C, V,
  std::void_t<decltype(std::declval<const C&>().find(std::declval<const V&>()))>> = true;

template<class C, class V> 
bool has_val(const C& c, const V& v) {
  auto end = std::end(c);
  if constexpr (use_mem_find<C, V>)
    return c.find(v) != end;
  else
    return std::find(std::begin(c), end, v) != end;
}

Demo.

  • Related