Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...