问题
For example, I have a convert template (any existing library to do it?)
template<class T> struct Convert;
template<> struct Convert<T0> {typedef C0 Type;};
template<> struct Convert<T1> {typedef C1 Type;};
template<> struct Convert<T2> {typedef C2 Type;};
From the convert, It convert
std::tuple<T0, T1, T2>; // version A
To
std::tuple<C0, C1, C2>; // version B
Any way to do it generally, like
template<class tupleA, template<class> class Convert>
{
typedef .... tupleB;
}
Some other questions: (1) Can I get its variadic parameters from a specific tuple? (2) If so, can I use convert on the variadic parameters?
回答1:
Try this:
template <typename... Args>
struct convert;
template <typename... Args>
struct convert<std::tuple<Args...>>
{
typedef std::tuple<typename Convert<Args>::Type...> type;
};
Here is an example program:
#include <type_traits>
#include <tuple>
template<class T> struct Convert;
template<> struct Convert<int> {typedef bool Type;};
template<> struct Convert<char> {typedef int Type;};
template<> struct Convert<bool> {typedef char Type;};
template <typename... Args>
struct convert;
template <typename... Args>
struct convert<std::tuple<Args...>>
{
typedef std::tuple<typename Convert<Args>::Type...> type;
};
int main()
{
static_assert(
std::is_same<
convert<std::tuple<int, char, bool>>::type,
std::tuple<bool, int, char>
>::value, ""
);
}
Demo
来源:https://stackoverflow.com/questions/17618938/how-to-make-a-tuple-from-a-tuple