Elegantly define multi-dimensional array in modern C++

后端 未结 1 1291
遇见更好的自我
遇见更好的自我 2020-12-24 14:12

Defining multi-dimensional array using the T[][][] syntax is easy. However, this creates a raw array type which doesn\'t fit nicely into modern C++. That\'s why

1条回答
  •  失恋的感觉
    2020-12-24 14:44

    Refer to Multi-dimensional arrays in C++11

    template 
    struct MultiDimArray 
    {
      using Nested = typename MultiDimArray::type;
      // typedef typename MultiDimArray::type Nested;
      using type = std::array;
      // typedef std::array type;
    };
    
    template 
    struct MultiDimArray 
    {
      using type = std::array;
      // typedef std::array type;
    };
    
    MultiDimArray::type floats;
    

    MultiDimArray is a pair of meta-functions to compute nested type for multi-dimensional std::array. The most general MultiDimArray is a variadic template of unsigned integers to pass an arbitrary number of dimensions. The terminating MultiDimArray specialization defines the simplest case of single dimensional std::array.

    0 讨论(0)
提交回复
热议问题