【发布时间】:2017-02-28 07:15:42
【问题描述】:
我正在寻找一种方法来比较两个元组,看看它们是否包含相同的类型。
类型的顺序无关紧要。只要两个元组的类型之间存在一对一的映射,我就会认为它们是等价的。
这是我设置的一个小测试。
我在执行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
}
【问题讨论】:
-
我想知道您是否可以使用this 对元组类型进行“排序”,然后您可以遍历这些类型以确保它们是相同的类型。
-
换句话说,你想要一个编译时
is_permutation。
标签: c++ comparison tuples c++14 stdtuple