c++ - Determining a template pack from multiple inheritance -
consider code:
enum brand {a,b,c,d,e,f,g,h,i,j}; template <brand, int> struct {}; struct b : a<a,4>, a<d,0>, a<i,3> {}; struct c : a<b,0>, a<c,5>, a<e,1>, a<h,4>, a<j,0> {}; template <typename, brand...> void foo() {} int main() { foo<b, a,d,i>(); foo<c, b,c,e,h,j>(); }
foo<b, a,d,i>();
makes sense in (real) program if has a,d,i
parameters, because of b's multiple inheritance. however, a,d,i
should deducible somehow, otherwise there maintenance problems if ever change b's inheritance. same thing goes foo<c, b,c,e,h,j>();
i'm stuck writing out template <typename t> struct getbrands
obtain deducible pack of brand
elements t
. appreciated. now, let's assume highest int value in 10.
what if turned problem on head , (slightly) changed way write definitions of b
, c
classes?
below, i've borrowed test code answer:
#include <iostream> enum brand {a,b,c,d,e,f,g,h,i,j}; template<brand, int> struct { }; template<typename...> struct a_base; template<brand... brands, int... is> struct a_base<a<brands, is>...> : a<brands, is>... { }; struct b : a_base<a<a,4>, a<d,0>, a<i,3>> { }; struct c : a_base<a<b,0>, a<c,5>, a<e,1>, a<h,4>, a<j,0>> { }; //template<typename, brand...> void foo() { } // *overloading* foo in order test outputs. template<typename, brand x, brand y, brand z> void foo() { std::cout << x << ' ' << y << ' ' << z << '\n'; } template<typename, brand x, brand y, brand z, brand u, brand v> void foo() { std::cout << x << ' ' << y << ' ' << z << ' ' << u << ' ' << v << '\n'; } template<typename s, brand... brands, int... is> void foo_helper(a_base<a<brands, is>...>) { foo<s, brands...>(); } template<typename s> void bar() { foo_helper<s>(s()); } int main() { bar<b>(); // supposed same foo<b, a,d,i>(); // 0 3 8 bar<c>(); // supposed same foo<c, b,c,e,h,j>(); // 1 2 4 7 9 }
i think can adapted handle more general cases need.
Comments
Post a Comment