Given:
boost::variant<T1,T2,T3,...,TN>
Calculate the following at compile time:
max(sizeof(T1), sizeof(T2), sizeof(T3),... ,sizeof(TN))
I had no idea how to approach this, but this answer clarified how I can start. Using the code in this answer with two types T1and T2, I could use the following in the source file to get the size of a larger object:
size_t largestSize = sizeof(largest<T1, T2>::type);
This is exactly what I would like to do, but I need a template largestfor working with more than two classes - in particular, it will need to check all types stored in the object boost::variant.
I know I boost::varianthave a typestypedef that defines some kind of list of types in a variant. The problem is that I am completely lost when I try to trick all the elements boost::mplinto the implementation. I don’t intuitively understand what it boost::variant::typesis and how I can pass it into my own template that does something with it.
In my head, it will look like the final implementation:
typedef boost::variant<T1, T2, T3, T4> MyVariant;
size_t largestSize = sizeof(largest<MyVariant::types>::type);
Unfortunately, I have no idea how to implement this version largest.
I'm not sure if this is a reasonable approach, so I am open to any other ways to achieve this (maybe apply boost::static_visitorto all types at compile time?).