简体   繁体   中英

How to define type trait for containers with index operator?

I have the following type traits to distinguish between fundamental and container types:

template <typename T>
using enable_if_fundamental_t = std::enable_if_t<std::is_fundamental_v<T>>;

template <typename T, typename = void>
struct is_container : std::false_type {};

template <typename T>
struct is_container<
      T
    , std::void_t<
          typename T::value_type
        , typename T::size_type
        , typename T::allocator_type
        , typename T::iterator
        , typename T::const_iterator
        , decltype(std::declval<T>().size())
        , decltype(std::declval<T>().begin())
        , decltype(std::declval<T>().end())
        , decltype(std::declval<T>().cbegin())
        , decltype(std::declval<T>().cend())
        >
    > : std::true_type {};

template <typename T>
constexpr bool is_container_v = is_container<T>::value;

template <typename T>
using enable_if_container_t = std::enable_if_t<is_container_v<T>>;

and they are used with the following functions:

template <typename T, typename = enable_if_fundamental_t<T>>
void foo(T)
{
    std::cout << "This is a fundamental type" << std::endl;
}

template <typename C, typename = enable_if_container_t<C>>
void foo(const C& c)
{
    std::cout << "This is a container type" << std::endl;
}

with the following arguments:

std::list<std::uint32_t> l;
std::vector<std::uint32_t> v;
std::map<std::string, std::uint32_t> m;
std::unordered_map<std::string, std::uint32_t> um;
std::uint32_t i = 42;

foo(l);
foo(v);
foo(m);
foo(um);
foo(i);

and they work fine.

Now I want to distinguish containers which has overloaded operator[] from the others. I tried the following code:

template <typename T, typename = void>
struct is_container_with_index_operator_with_size_type : std::false_type {};

template <typename T>
struct is_container_with_index_operator_with_size_type<
      T
    , std::void_t<
          enable_if_container_t<T>
        , decltype(std::declval<T>().operator[](std::declval<typename T::size_type>()))
        >
    > : std::true_type {};

template <typename T>
constexpr bool is_container_with_index_operator_with_size_type_v =
    is_container_with_index_operator_with_size_type<T>::value;

template <typename T, typename = void>
struct is_container_with_index_operator_with_key_type : std::false_type {};

template <typename T>
struct is_container_with_index_operator_with_key_type<
      T
    , std::void_t<
          enable_if_container_t<T>
        , typename T::key_type
        , decltype(std::declval<T>().operator[](std::declval<typename T::key_type>()))
        >
    > : std::true_type {};

template <typename T>
constexpr bool is_container_with_index_operator_with_key_type_v =
    is_container_with_index_operator_with_key_type<T>::value;

template <typename T>
constexpr bool is_container_with_index_operator_v =
    is_container_with_index_operator_with_size_type_v<T> ||
    is_container_with_index_operator_with_key_type_v<T>;

template <typename T>
constexpr bool is_container_without_index_operator_v =
    is_container_v<T> &&
    !is_container_with_index_operator_v<T>;

template <class T>
using enable_if_container_with_index_operator_t =
    std::enable_if_t<is_container_with_index_operator_v<T>>;

template <class T>
using enable_if_container_without_index_operator_t =
    std::enable_if_t<is_container_without_index_operator_v<T>>;

with the following overloads:

template <typename T, typename = enable_if_fundamental_t<T>>
void foo(T)
{
    std::cout << "This is a fundamental type" << std::endl;
}

template <typename C, typename = enable_if_container_without_index_operator_t<C>>
void foo(const C&)
{
    std::cout << "This is a container type without index operator" << std::endl;
}

template <typename C, typename = enable_if_container_with_index_operator_t<C>>
void foo(const C&)
{
    std::cout << "This is a container type with index operator" << std::endl;
}

with the same arguments as above, bit it produced me the error:

error C2995: 'void foo(const C &)': function template has already been defined

I tried several variations of the code above, but I didn't manage to do it the right way.

How to do this the right way and is it possible to achieve a simpler code? For example without separate meta functions for containers which use size_type and key_type for the operator[] ?

I'm using Visual Studio 2017 version 15.7.2 with v141 toolset and enabled /std:c++17 .

That's because you can't overload functions based on default template parameter values alone. You can reproduce that with:

template <typename T, typename = std::enable_if_t<(sizeof(T) > 2)>> void foo() {}
template <typename T, typename = std::enable_if_t<!(sizeof(T) > 2)>> void foo() {}
// error: redefinition of 'template<class T, class> void foo()'

A possible solution is to use enable_if_t in a type of a template parameter:

template <typename T, std::enable_if_t<(sizeof(T) > 2), int> = 0> void foo() {}
template <typename T, std::enable_if_t<!(sizeof(T) > 2), int> = 0> void foo() {}

Or in return type:

template <typename T> std::enable_if_t<(sizeof(T) > 2)/*,void*/> foo() {}
template <typename T> std::enable_if_t<!(sizeof(T) > 2)/*,void*/> foo() {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM