Detect if type is "mapping"

I would like to parse C ++ containers in another object using my type ::iterator. Containers that use an iterator member type point to an object of the same type (vectors, queues, etc.), turn into a list-like object, and containers whose iterator member type points to std::pair, turn into a map-like object .

I am trying to write a member function to detect the last kind of container, but it does not work. Here is what I still have:

#include <tuple>
#include <iterator>
#include <type_traits>

template <typename T>
struct is_pair : std::false_type { };

template <typename T, typename U>
struct is_pair<std::pair<T, U>> : std::true_type { };

template <typename T>
constexpr bool is_pair_v = is_pair<T>::value;

template <typename...>
struct is_mapping : std::false_type { };

template <typename Container>
struct is_mapping<Container, std::enable_if_t<
    is_pair_v<std::iterator_traits<typename Container::iterator>::value_type>
>> : std::true_type { };

template <typename T>
constexpr bool is_mapping_v = is_mapping<T>::value;

#include <map>
#include <vector>
#include <iostream>

int main() {
    std::cout << "is_pair:" << std::endl;
    std::cout << "Map:    " << is_pair_v<std::iterator_traits<std::map<int, int>::iterator>::value_type> << std::endl;
    std::cout << "Vector: " << is_pair_v<std::iterator_traits<std::vector<int>::iterator>::value_type>   << std::endl;
    std::cout << std::endl;
    std::cout << "is_mapping:" << std::endl;
    std::cout << "Map:    " << is_mapping_v<std::map<int, int>> << std::endl;
    std::cout << "Vector: " << is_mapping_v<std::vector<int>>   << std::endl;
}

For some reason, is_mapping_valways false, and the code outputs this result:

$ g++ -std=c++14 temp.cc && ./a.out
is_pair:
Map:    1
Vector: 0

is_mapping:    
Map:    0
Vector: 0

What is wrong with my code?


: , . ::key_type ::mapped_type ( , std::multimap). , std::pair , .

+4
2
is_pair_v<std::iterator_traits<typename Container::iterator>::value_type>

is_pair_v<typename std::iterator_traits<typename Container::iterator>::value_type>

value_type - . typename enable_if, .

, main , , , , value_type .

-

template<typename...>

template<typename, typename = void>

is_mapping<T> , .

Live

+5

. :

  • enable_if off is_pair_v<std::iterator_traits<typename Container::iterator>::value_type>. std::iterator_traits<...>::value_type , SFINAE , .
  • template <typename...> , . , , is_mapping<T> is_mapping<T> , is_mapping<T, void> . , is_mapping<T> .

    , template <typename, typename = void>:

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

template <typename Container>
struct is_mapping<Container, std::enable_if_t<
    is_pair_v<typename std::iterator_traits<typename Container::iterator>::value_type>
>> : std::true_type { };

is_mapping :

template <typename Container>
struct is_mapping : is_pair<
    typename std::iterator_traits<typename Container::iterator>::value_type
> {};

,

if (is_pair(...)) return true;
else return false;

return is_pair(...)
+2

Source: https://habr.com/ru/post/1681212/


All Articles