Question

I'm writing some container manipulation functions. It is often the case that there's one version for things like vector-like containers such as vector, list, deque, array, etc. and another version for associative containers like map, multimap, unordered_map, etc. I was wondering what is the "best way" to detect whether a class is an associative container. Maybe something like detecting the existence of the mapped_type typedef with BOOST_MPL_HAS_XXX_TRAIT_DEF?

Was it helpful?

Solution

It's a compile time test, so there's no CPU/memory efficiency aspect to select the "best way". If you have it working by checking mapped_type with boost, and that suits your needs, there's no reason to look for anything different, though there are certainly boost-free alternatives (e.g. see here)

Note though that set and unordered_set are deemed associative containers by the Standard, but do not have a mapped_type member - if you want to include them you could test for key_type.

OTHER TIPS

I would not go that way of assumption. Be specific and specialize a template.

I do this:

// is_deque
// ========

template<typename T, typename ... Types>
struct is_deque {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_deque<std::deque<Types...>> {
    static constexpr bool value = true;
};


// is_forward_list
// ===============

template<typename T, typename ... Types>
struct is_forward_list {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_forward_list<std::forward_list<Types...>> {
    static constexpr bool value = true;
};


// list
// ====

template<typename T, typename ... Types>
struct is_list {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_list<std::list<Types...>> {
    static constexpr bool value = true;
};


// vector
// ======

template<typename T, typename ... Types>
struct is_vector {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_vector<std::vector<Types...>> {
    static constexpr bool value = true;
};


// map
// ===

template<typename T, typename ... Types>
struct is_map {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_map<std::map<Types...>> {
    static constexpr bool value = true;
};


// set
// ===

template<typename T, typename ... Types>
struct is_set {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_set<std::set<Types...>> {
    static constexpr bool value = true;
};


// unordered_map
// =============

template<typename T, typename ... Types>
struct is_unordered_map {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_unordered_map<std::unordered_map<Types...>> {
    static constexpr bool value = true;
};


// unordered_set
// =============

template<typename T, typename ... Types>
struct is_unordered_set {
    static constexpr bool value = false;
};


template<typename ... Types>
struct is_unordered_set<std::unordered_set<Types...>> {
    static constexpr bool value = true;
};


// is_sequence_container
// =====================

template <typename T>
struct is_sequence_container {
    static constexpr bool value
        =  is_deque<T>::value
        || is_forward_list<T>::value
        || is_list<T>::value
        || is_vector<T>::value;
};


// is_associative_container
// ========================

template <typename T>
struct is_associative_container {
    static constexpr bool value
        =  is_map<T>::value
        || is_set<T>::value;
};


// is_unordered_associative_container
// ==================================

template <typename T>
struct is_unordered_associative_container {
    static constexpr bool value
        =  is_unordered_map<T>::value
        || is_unordered_set<T>::value;
};


// is_container
// ============

template <typename T>
struct is_container {
    static constexpr bool value
        =  is_sequence_container<T>::value
        || is_associative_container<T>::value
        || is_unordered_associative_container<T>::value;
};

I know that the question was asked 5 years ago, but this is what I did without any requirements beyond c++11:

/// @brief container traits
////////////////////////////////////////////////////////////////////////////////

namespace container_traits {

using tc = char[2];

template<typename T> struct is_container {
  static tc& test(...);

  template <typename U>
  static char test(U&&, decltype(std::begin(std::declval<U>()))* = 0);
  static constexpr bool value = sizeof(test(std::declval<T>())) == 1;
};

template < typename T > struct is_associative {
  static tc& test(...) ;

  template < typename U >
  static char test(U&&, typename U::key_type* = 0) ;
  static constexpr bool value = sizeof( test( std::declval<T>() ) ) == 1 ;
};

}

template < typename T > struct is_container :
  std::conditional<(container_traits::is_container<T>::value || std::is_array<T>::value)
                   && !std::is_same<T, std::string>::value
                   && !std::is_same<T, const std::string>::value, std::true_type, std::false_type >::type {};

template < typename T > struct is_associative :
  std::conditional< container_traits::is_container<T>::value && container_traits::is_associative<T>::value,  std::true_type, std::false_type >::type {};



////////////////////////////////////////////////////////////////////////////////
/// @brief no std::enable_if_t in c++11
////////////////////////////////////////////////////////////////////////////////

#if __cplusplus <= 201103L
namespace std {
template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;
}
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top