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
}
Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.
Tuples are compared position by position means the first item of a tuple compared with the first item of the second tuple and so on. If all the items will equal then the result will be true.
Practical Data Science using Python When it is required to check if two list of tuples are identical, the '==' operator is used. The '==' operator checks to see if two iterables are equal or not.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With