Is there any way in C++ define a type that is big enough to hold at most a specific number, presumably using some clever template code. For example I want to be able to writ
How about a conditional:
#include
#include
template
struct MinInt
{
typedef typename std::conditional< N < std::numeric_limits::max(),
unsigned char, std::conditional< N < std::numeric_limits::max(),
unsigned short int>::type,
void*>::type>::type
type;
};
This would have to be extended to encompass all desired types, in order; at the final stage you could use enable_if
rather than conditional
to have an instantiation error right there if the value is too large.