在元组中生成类型对
Generating pairs of types inside a tuple
有一个类型元组,我想生成包含所有可能对的元组。我无法理解为什么在第一个位置具有最后类型的对(如:{B,_})出现多次。
#include <iostream>
#include <type_traits>
#include <tuple>
#include <utility>
struct A; struct B; struct C; struct D; struct E; struct F;
using Types = std::tuple<A,B>;
template <typename TupleAll, typename Tuple1, typename Tuple2>
struct PairsGenerator {
using Type = std::tuple<>;
};
template <typename TupleAll, typename T, typename U, typename... Ts, typename... Us>
struct PairsGenerator<TupleAll, std::tuple<T, Ts...>, std::tuple<U, Us...>> {
using Type = decltype(std::tuple_cat(
std::declval<std::tuple<std::pair<T, U>>>(),
std::declval<typename PairsGenerator<TupleAll, std::tuple<T, Ts...>, std::tuple<Us...>>::Type>(),
std::declval<typename PairsGenerator<TupleAll, std::tuple<Ts...>, TupleAll>::Type>()));
};
template <typename TypesTuple>
using Pairs = typename PairsGenerator<TypesTuple, TypesTuple, TypesTuple>::Type;
int main() {
//std::cout << typeid(decltype(std::declval<Pairs<Types>>())).name() << std::endl;
std::cout << std::boolalpha << std::is_same_v<std::tuple<
std::pair<A,A>,
std::pair<A,B>,
std::pair<B,A>,
std::pair<B,B>,
std::pair<B,A>,
std::pair<B,B>
>, Pairs<Types>>;
}
Returns 是的,反而应该是,但是下面是假的
std::cout << std::boolalpha << std::is_same_v<std::tuple<
std::pair<A,A>,
std::pair<A,B>,
std::pair<B,A>,
std::pair<B,B>
>, Pairs<Types>>;
这一行
std::declval<typename PairsGenerator<TupleAll, std::tuple<Ts...>, TupleAll>::Type>()));
第二个TupleAll
应该是std::tuple<U, Us...>
。就像现在一样,如果您的输入长度大于 2,您的代码会做很多更奇怪的事情。对我来说,您似乎可以完全摆脱 TupleAll
参数。
有一个类型元组,我想生成包含所有可能对的元组。我无法理解为什么在第一个位置具有最后类型的对(如:{B,_})出现多次。
#include <iostream>
#include <type_traits>
#include <tuple>
#include <utility>
struct A; struct B; struct C; struct D; struct E; struct F;
using Types = std::tuple<A,B>;
template <typename TupleAll, typename Tuple1, typename Tuple2>
struct PairsGenerator {
using Type = std::tuple<>;
};
template <typename TupleAll, typename T, typename U, typename... Ts, typename... Us>
struct PairsGenerator<TupleAll, std::tuple<T, Ts...>, std::tuple<U, Us...>> {
using Type = decltype(std::tuple_cat(
std::declval<std::tuple<std::pair<T, U>>>(),
std::declval<typename PairsGenerator<TupleAll, std::tuple<T, Ts...>, std::tuple<Us...>>::Type>(),
std::declval<typename PairsGenerator<TupleAll, std::tuple<Ts...>, TupleAll>::Type>()));
};
template <typename TypesTuple>
using Pairs = typename PairsGenerator<TypesTuple, TypesTuple, TypesTuple>::Type;
int main() {
//std::cout << typeid(decltype(std::declval<Pairs<Types>>())).name() << std::endl;
std::cout << std::boolalpha << std::is_same_v<std::tuple<
std::pair<A,A>,
std::pair<A,B>,
std::pair<B,A>,
std::pair<B,B>,
std::pair<B,A>,
std::pair<B,B>
>, Pairs<Types>>;
}
Returns 是的,反而应该是,但是下面是假的
std::cout << std::boolalpha << std::is_same_v<std::tuple<
std::pair<A,A>,
std::pair<A,B>,
std::pair<B,A>,
std::pair<B,B>
>, Pairs<Types>>;
这一行
std::declval<typename PairsGenerator<TupleAll, std::tuple<Ts...>, TupleAll>::Type>()));
第二个TupleAll
应该是std::tuple<U, Us...>
。就像现在一样,如果您的输入长度大于 2,您的代码会做很多更奇怪的事情。对我来说,您似乎可以完全摆脱 TupleAll
参数。