Is static_cast<T>(-1) the right way to generate all-one-bits data without numeric_limits?

Use the bitwise NOT operator ~ on 0.

T allOnes = ~(T)0;

A static_cast<T>(-1) assumes two's complement, which is not portable. If you are only concerned about unsigned types, hvd's answer is the way to go.

Working example: https://ideone.com/iV28u0


Focusing on unsigned integral types, what do I put there? Specifically, is static_cast(-1) good enough

If you're only concerned about unsigned types, yes, converting -1 is correct for all standard C++ implementations. Operations on unsigned types, including conversions of signed types to unsigned types, are guaranteed to work modulo (max+1).


This disarmingly direct way.

T allOnes;
memset(&allOnes, ~0, sizeof(T));