/* Sometimes you need to calculate at compile time the biggest object to statically allocate pools or arrays. The Big template returns the size of the biggest object. The BigT template returns the type of the biggest object. As they are recursive template, you can use them with as many types as you want ( see sample use ). This is probably common knowledge but it took use quite a while to figure out a good way to do it without macros .... */ // Return the size of the biggest type. template class Big { public: static const unsigned int Res = sizeof(a) > sizeof(b) ? sizeof(a) : sizeof(b); }; template class Big,c> { public: static const unsigned int Res = Big::Res > sizeof(c) ? Big::Res : sizeof(c); }; template class Rank { public: typedef a Res; }; template class Rank { public: typedef b Res; }; // Return the type of the biggest type. template class BigT { public: typedef Rank sizeof(b))>::Res Res; }; template class BigT,c> { public: typedef Rank,c,(sizeof(BigT::Res) > sizeof(c))>::Res Res; }; // Sample use. class a { int m_a; }; class b { short m_b; }; class c { char m_c; }; // Allocate the memory needed to store 8 of the biggest from a,b,c. const int BiggestTypeSize = Big,c>::Res; char MyMem[BiggestTypeSize * 8]; // Allocate an array of 8 elements of the biggest from a,b,c. typedef BigT,c>::Res BiggestType; BiggestType MyArray[8]; // Unit test. static bool TestA = Big,c>::Res == sizeof(int); // Should be 1. static bool TestB = sizeof(BigT,c>::Res) == sizeof(int); // Should be 1. Laurent Benadiba & Dushan Leska