Odgrzewam troche temat, troche go komplikujac... zalozmy, ze nie mam prostego mapowania base<T> -> instance<T> tylko chce zrobic "powazniejsza" konwersje. Powiedzmy typ base ma wiecej meta parametrow.
template<typename A, typename B, int C> struct base {};
i na jego podstawie chce budowac tuple typow instance wybierajac (oczywiscie podczas kompilacji;)) sobie ktorys z tych parametrow, np B, tj.: base<A, B, C> -> instance<B>
. Lub tez nawet uzywac do konstrukcji instancji typow instance elementow typu base, np. instance<typename T>
ma pole value
(powiedzmy int). Obrazowo: base<A, B, C> -> instance<B>(C)
.
edit: na razie mam tyle:
#include <iostream>
#include <tuple>
using namespace std;
template<typename A, typename B, int C>
struct base {
static constexpr auto Value = C;
using type_A = A;
using type_B = B;
};
template<typename T>
struct instance {
instance () : v (0) {}
instance (const T& v) : v(v) {}
int v;
};
template<typename T, size_t N>
struct type_generator
{
using rest_tuple = typename type_generator<T, N-1>::type;
using current_type = instance<typename std::tuple_element<N, T>::type::type_A>;
using type = decltype(
std::tuple_cat(
std::declval<rest_tuple>(),
std::declval<std::tuple<current_type>>()
)
);
};
template<typename T>
struct type_generator<T, 0>
{
using current_type = instance<typename std::tuple_element<0, T>::type::type_A>;
using type = std::tuple<current_type>;
};
template<typename M>
auto instantiate(const M &m) -> typename type_generator<M, std::tuple_size<M>::value - 1>::type
{
}
int main() {
tuple<base<int, float, 1>, base<float, double, 2>> a;
static_assert(
std::is_same<
type_generator<decltype(a), tuple_size<decltype(a)>::value - 1>::type,
tuple<instance<int>, instance<float>>
>::value == true);
return 0;
}
zawiesilem sie na implementacji instantiate(), chce wziac do inicjalizacji obiektow instance wartosc Value z base.