简体   繁体   中英

How do I compare tuples for equivalent types disregarding type order?

I'm looking for a way to compare two tuples to see if they contain the same types.
The order of the types does not matter. As long as there is a one to one mapping between the types of the two tuples, I will consider them equivalent.

Here is a small test I have set up.
I am having trouble implementing equivalent_types() :

#include <iostream>
#include <utility>
#include <tuple>
#include <functional>

template <typename T, typename U>
bool equivalent_types(T t, U u){
    return (std::tuple_size<T>::value == std::tuple_size<U>::value);
    //&& same types regardless of order
}


int main() {

    //these tuples have the same size and hold the same types.
    //regardless of the type order, I consider them equivalent.  
    std::tuple<int,float,char,std::string> a;
    std::tuple<std::string,char,int,float> b;

    std::cout << equivalent_types(a,b) << '\n'; //should be true
    std::cout << equivalent_types(b,a) << '\n'; //should be true

    //examples that do not work:  

    //missing a type (not enough types)
    std::tuple<std::string,char,int> c;

    //duplicate type (too many types)
    std::tuple<std::string,char,int,float,float> d;

    //wrong type
    std::tuple<bool,char,int,float> e;

    std::cout << equivalent_types(a,c) << '\n'; //should be false
    std::cout << equivalent_types(a,d) << '\n'; //should be false
    std::cout << equivalent_types(a,e) << '\n'; //should be false
}

By counting types of both tuples, you may do something like:

template <typename T, typename Tuple>
struct type_counter;

template <typename T, typename ... Ts>
struct type_counter<T, std::tuple<Ts...>> :
    std::integral_constant<std::size_t, (... + std::is_same<T, Ts>::value)> {};

template <typename Tuple1, typename Tuple2, std::size_t... Is>
constexpr bool equivalent_types(const Tuple1&, const Tuple2&, std::index_sequence<Is...>)
{
    return (...
            && (type_counter<std::tuple_element_t<Is, Tuple1>, Tuple1>::value
               == type_counter<std::tuple_element_t<Is, Tuple1>, Tuple2>::value));
}

template <typename Tuple1, typename Tuple2>
constexpr bool equivalent_types(const Tuple1& t1, const Tuple2& t2)
{
    constexpr auto s1 = std::tuple_size<Tuple1>::value;
    constexpr auto s2 = std::tuple_size<Tuple2>::value;

    return s1 == s2
      && equivalent_types(t1, t2, std::make_index_sequence<std::min(s1, s2)>());
}

Demo C++17
Demo C++14

I use c++17 for folding expression but it can be rewritten as constexpr function easily.

With Hana (packaged with recent Boost versions), we can convert each tuple type into a map from types to the number of times they occur and then comparing those maps for equality:

template <typename T, typename U>
bool equivalent_types(T t, U u) {
    namespace hana = boost::hana;
    auto f = [](auto m, auto&& e) {
        auto k = hana::decltype_(&e);
        return hana::insert(hana::erase_key(m, k),
            hana::make_pair(k, hana::find(m, k).value_or(0) + 1));
    };
    return hana::fold(t, hana::make_map(), f) == hana::fold(u, hana::make_map(), f);
}

Example .

Note that &e as the argument to hana::decltype_ is necessary to ensure that eg int and int& are treated as different types (ditto with passing e by universal reference).

This code appears to work with the parameters in any order. The false result is a compiler error. I'm not great with TMP yet, but it is 100% compile-time.. I'd love some suggestions on how to clean this up. Live: https://godbolt.org/g/3RZaMQ

#include <tuple>
#include <type_traits>
using namespace std;

// This struct removes the first instance of TypeToRemove from the Tuple or 'returns' void if it isn't present
template<class TypeToRemove, class ProcessedTupleParts, class RemainingTuple, class=void>
struct RemoveType;

template<class T, class... ProcessedTupleParts, class TupleHead, class... TupleTail>
struct RemoveType<T, std::tuple<ProcessedTupleParts...>, std::tuple<TupleHead, TupleTail...>, enable_if_t<std::is_same<T, TupleHead>::value>> {
    using RemovedType = std::tuple<ProcessedTupleParts..., TupleTail...>;
};

template<class T, class... ProcessedTupleParts, class TupleHead, class... TupleTail>
struct RemoveType<T, std::tuple<ProcessedTupleParts...>, std::tuple<TupleHead, TupleTail...>, enable_if_t<!std::is_same<T, TupleHead>::value>> {
    using RemovedType = typename RemoveType<T, std::tuple<ProcessedTupleParts..., TupleHead>, std::tuple<TupleTail...>>::RemovedType;
};

template<class T, class... Anything>
struct RemoveType<T, std::tuple<Anything...>, std::tuple<>> {
    using RemovedType = void;
};

template<class T1, class T2>
struct CompareTuples;

template<class T1Head, class... T1Tail, class T2>
struct CompareTuples<std::tuple<T1Head, T1Tail...>, T2> {
    using Result = typename CompareTuples<std::tuple<T1Tail...>, typename RemoveType<T1Head, std::tuple<>, T2>::RemovedType>::Result;
};

template<>
struct CompareTuples<std::tuple<>, std::tuple<>> {
    using Result = std::tuple<>;
};


template<class... T2Body>
struct CompareTuples<std::tuple<>, std::tuple<T2Body...>> {
    using Result = void;
};

template<class T1>
struct CompareTuples<T1, void> {
    using Result = void;
};



int main() {
    RemoveType<int, std::tuple<>,
    RemoveType<char, std::tuple<>, std::tuple<int, char>>::RemovedType>::RemovedType aa;

    CompareTuples<std::tuple<int>, std::tuple<int>>::Result a;
    CompareTuples<std::tuple<char, int>, std::tuple<int, char>>::Result b;
    CompareTuples<std::tuple<char, int>, std::tuple<int, char, double>>::Result e;
    CompareTuples<std::tuple<char, double, int>, std::tuple<int, char, double>>::Result f;
    CompareTuples<std::tuple<char, double, int>, std::tuple<int, char>>::Result g;
    CompareTuples<std::tuple<char>, std::tuple<int>>::Result c;
    CompareTuples<std::tuple<int>, std::tuple<int, char>>::Result d;

}

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