Suppose I need to make a template with a member of exactly N
bits in length, where N
is the template parameter. I could of course define something like this
#include <cstdint>
template<int N>
struct sized_uint {};
template<> struct sized_uint<8> { typedef uint8_t type; };
template<> struct sized_uint<16> { typedef uint16_t type; };
template<> struct sized_uint<32> { typedef uint32_t type; };
template<> struct sized_uint<64> { typedef uint64_t type; };
and then use it in my template for e.g. a function:
template<int N> void myfunc(typename sized_uint<N>::type);
But are there any standard types like the above defined sized_uint
in any version of C++?
There is no standard type like that. However, there is boost::int_t, which will do what you want if the boost dependency is acceptable for you. Note that the semantics are slightly different, in that boost::int_t
will give you the smallest integral type with at least that many bits, rather than exactly that many.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With