In the code below, C's base class B1's template argument OFFSET depends on B0, and B2 on B1.
This is done by manual write the code every time an instance of C is created (in the main method). Is there a way to move this functionality to the definition of C instead?
template<int OFFSET>
struct A {
enum O { offset = OFFSET };
enum S { size = 2 };
};
template<int OFFSET>
struct B {
enum O { offset = OFFSET };
enum S { size = 4 };
};
template < typename B0, typename B1, typename B2 >
struct C : public B0, B1, B2 {
};
int main(int argc, const char *argv[])
{
// instance of C
C< A<1>,
B< A<1>::offset * A<1>::size >,
A<
B< A<1>::offset * A<1>::size >::offset *
B< A<1>::offset * A<1>::size >::size
>
> c1;
// does the same thing
C< A<1>,
B< A<1>::size >,
A<
A<1>::size *
B< A<1>::size >::size
>
> c2;
return 0;
}
EDIT:
To answer the comments, here are the steps I think needs to be taken to solve this:
Write a metafunction which can change the offset: set_new_offset which for T defines the type T<2>
Use boost::mpl::times to calculate the new offsets
Add more template magic...