c++11 - C++ Array type dependent on template type -


i have class has integral template parameter n.

template<unsigned int n> class test { } 

now want have std::vector integral type small possible hold n bits.

e.g.

class test<8> {     std::vector<uint8_t> data; }  class test<9> {     std::vector<uint16_t> data; }  class test<10> {     std::vector<uint16_t> data; } ... 

is there better way n=1 n=64?

what using conditional?

#include <vector> #include <cstdint> #include <iostream> #include <type_traits>  template <std::size_t n> struct foo  {    static_assert( n < 65u, "foo 64 limit");     using vtype = typename std::conditional<       (n < 9u), std::uint8_t,         typename std::conditional< (n < 17u), std::uint16_t,            typename std::conditional< (n < 33u), std::uint32_t, std::uint64_t    >::type>::type>::type;     std::vector<vtype> data;  };  int main()  {    static_assert( 1u == sizeof(foo<1>::vtype), "!");    static_assert( 1u == sizeof(foo<8>::vtype), "!");    static_assert( 2u == sizeof(foo<9>::vtype), "!");    static_assert( 2u == sizeof(foo<16>::vtype), "!");    static_assert( 4u == sizeof(foo<17>::vtype), "!");    static_assert( 4u == sizeof(foo<32>::vtype), "!");    static_assert( 8u == sizeof(foo<33>::vtype), "!");    static_assert( 8u == sizeof(foo<64>::vtype), "!");     // foo<65> f65; compilation error  } 

or, in more elegant way (imho), can define type traits (selecttypebydim, in following example) select first useful type in list

#include <tuple> #include <vector> #include <cstdint> #include <climits> #include <type_traits>  template <std::size_t n, typename t,    bool = (n <= sizeof(typename std::tuple_element<0u, t>::type)*char_bit)> struct stbdh;  template <std::size_t n, typename t0, typename ... ts> struct stbdh<n, std::tuple<t0, ts...>, true>  { using type = t0; };  template <std::size_t n, typename t0, typename ... ts> struct stbdh<n, std::tuple<t0, ts...>, false>  { using type = typename stbdh<n, std::tuple<ts...>>::type; };  template <std::size_t n, typename ... ts> struct selecttypebydim : stbdh<n, std::tuple<ts...>>  { };  template <std::size_t n> struct foo  {    static_assert( n < 65u, "foo 64 limit");     using vtype = typename selecttypebydim<n,          std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t>::type;     std::vector<vtype> data;  };  int main()  {    static_assert( 1u == sizeof(foo<1u>::vtype), "!");    static_assert( 1u == sizeof(foo<char_bit>::vtype), "!");    static_assert( 2u == sizeof(foo<char_bit+1u>::vtype), "!");    static_assert( 2u == sizeof(foo<(char_bit<<1)>::vtype), "!");    static_assert( 4u == sizeof(foo<(char_bit<<1)+1u>::vtype), "!");    static_assert( 4u == sizeof(foo<(char_bit<<2)>::vtype), "!");    static_assert( 8u == sizeof(foo<(char_bit<<2)+1u>::vtype), "!");    static_assert( 8u == sizeof(foo<(char_bit<<3)>::vtype), "!");     //foo<(char_bit<<3)+1u> f65; compilation error  } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -