[英]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()
: 我在实现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 演示C ++ 17
Demo C++14 演示C ++ 14
I use c++17 for folding expression but it can be rewritten as constexpr function easily. 我使用c ++ 17来折叠表达式,但是可以很容易地将其重写为constexpr函数。
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: 使用Hana(与最新的Boost版本打包在一起),我们可以将每个元组类型转换成一个映射,从类型到它们发生的次数,然后比较这些映射是否相等:
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);
}
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). 请注意,必须使用&e
作为hana::decltype_
的参数,以确保将例如int
和int&
视为不同类型(通过通用引用传递e
的同上)。
This code appears to work with the parameters in any order. 该代码似乎可以按任何顺序与参数一起使用。 The false
result is a compiler error. false
结果是编译器错误。 I'm not great with TMP yet, but it is 100% compile-time.. I'd love some suggestions on how to clean this up. 我对TMP不太满意,但是它是100%的编译时。. 我很喜欢关于如何清理它的一些建议。 Live: https://godbolt.org/g/3RZaMQ 直播: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.