Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there standard integer types with sizes being template parameters?

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++?

like image 718
Ruslan Avatar asked Jul 21 '15 13:07

Ruslan


1 Answers

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.

like image 153
TartanLlama Avatar answered Nov 15 '22 12:11

TartanLlama