簡體   English   中英

如何使用索引運算符為容器定義類型特征?

[英]How to define type trait for containers with index operator?

我具有以下類型特征以區分基本類型和容器類型:

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>>;

它們與以下功能一起使用:

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;
}

具有以下參數:

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);

他們工作正常。

現在,我想將重載了operator[]容器與其他容器區分開。 我嘗試了以下代碼:

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>>;

具有以下重載:

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;
}

與上面相同的參數,它產生了錯誤:

錯誤C2995:“無效foo(const C&)”:函數模板已經定義

我嘗試了上面代碼的幾種變體,但是我沒有設法正確地做到這一點。

如何以正確的方式執行此操作,是否有可能實現更簡單的代碼? 例如,對於使用size_typekey_type作為operator[]容器,沒有單獨的元函數?

我正在將Visual Studio 2017版本15.7.2v141工具集一起使用並啟用/std:c++17

那是因為您不能僅基於默認模板參數值重載函數。 您可以使用以下方法重現該內容:

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()'

可能的解決方案是在模板參數的類型中使用enable_if_t

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() {}

或返回類型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM