我正在寻找一种比较两个元组的方法,看看它们是否包含相同的类型 类型的顺序无关紧要。只要两个元组的类型之间存在一对一的映射,我就会认为它们是等价的。
这是我设置的一个小测试
我无法实施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
}
答案 0 :(得分:3)
通过计算两个元组的类型,您可以执行以下操作:
drawTree.getEntries()
我使用c ++ 17进行折叠表达,但它可以很容易地重写为constexpr函数。
答案 1 :(得分:2)
使用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);
}
请注意,&e
作为hana::decltype_
的参数是必要的,以确保例如int
和int&
被视为不同的类型(通过引用传递e
同上)。
答案 2 :(得分:0)
此代码似乎以任何顺序使用参数。 false
结果是编译器错误。我对TMP不太满意,但它是100%的编译时间.. 我喜欢关于如何清理它的一些建议。直播: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;
}