You can use some pattern tricks:
#include <iostream> #include <climits> using namespace std; template<typename T, unsigned char Pattern, unsigned int N=sizeof(T)> struct FillInt { static const T Value=((T)Pattern)<<((N-1)*CHAR_BIT) | FillInt<T, Pattern, N-1>::Value; }; template<typename T, unsigned char Pattern> struct FillInt<T, Pattern, 0> { static const T Value=0; }; int main() { cout<<hex<<FillInt<unsigned int, 0xdc>::Value<<endl; // outputs dcdcdcdc on 32 bit machines }
which automatically adapts to the integral type passed as the first argument and is fully resolved at compile time, but this is just for fun, I donโt think I would use such a thing in real code.
source share