初始化一个constexpr std ::数组,其大小的n维std ::数组
Initialize a constexpr std::array with the size of an n-dimensional std::array
我编写了一个函数,该函数采用n维std::array
和一个等于输入std::array
尺寸的参数包(坐标)。我已经可以估计具有元功能的std ::数组的每个维度的大小,我写了一个函数,计算包装中的参数。
我要1)生成一个新的constexpr std::array
,其大小等于输入std::array
的尺寸数量。2)阵列应以输入std ::数组的每个维度的大小初始化。有人只用C 11即可填充std::array
的技巧。
例如。此代码
using array3d = std::array<std::array<std::array<int, 4>, 4>, 4>;
3d_helper<array3d>(array3d(), 0,0,0);
应生成:
constexpr std::array<int, 3> array = { 4, 4, 4 };
这是我到目前为止所拥有的:
//! Static estimation of std::array container size
// Declare a generic template (which is called initially)
template <size_t dim, class Array>
struct static_size;
// specialization for std::array and first dimension
// creates a struct with a static member "value = N"
template <class T, size_t N>
struct static_size<0, std::array<T, N>> : std::integral_constant<size_t, N> {};
// specialization for std::array and dimension > 0 -> recurse down in dim
template <size_t dim, class InnerArray, size_t N>
struct static_size<dim, std::array<InnerArray, N>> : static_size<dim - 1, InnerArray> {};
template <class FIRST, class... OTHER>
size_t num_args() {
return 1 + num_args<OTHER...>();
}
template <class FIRST>
size_t num_args() {
return 1;
}
template <class ARRAY, class... ARGS>
struct 3d_helper {
static glm::vec3 at_t(const ARRAY &points, ARGS... args) {
constexpr size_t nargs = num_args<ARGS...>();
/*
constexpr size_t n1 = static_size<0, ARRAY>::value - 1;
constexpr size_t n2 = static_size<1, ARRAY>::value - 1;
*/
// ...
using array_t = std::array<size_t, nargs>;
// fill it somehow
}
};
基于我对此相关问题的解决方案,这是一种做到这一点的方法
// Example program
#include <iostream>
#include <string>
#include <array>
// typedefs for certain container classes
template<class T, size_t x>
using array1D = std::array<T, x>;
template<class T, size_t x, size_t y>
using array2D = std::array<std::array<T, y>, x>;
template<class T, size_t x, size_t y, size_t z>
using array3D = std::array<std::array<std::array<T, z>, y>, x>;
template <size_t dim, typename Array>
struct size_of_dim;
// specialization for std array and first dimension
template <typename T, size_t N>
struct size_of_dim<0, std::array<T,N>> : std::integral_constant<size_t, N> {};
// specialization for std array and dimension > 0 → recurse down in dim
template <size_t dim, typename InnerArray, size_t N>
struct size_of_dim<dim, std::array<InnerArray,N>> : size_of_dim<dim-1,InnerArray> {};
template <typename Array>
struct cardinality : std::integral_constant<size_t, 0> {};
template <typename T, size_t N>
struct cardinality<std::array<T,N>> : std::integral_constant<size_t, cardinality<T>::value + 1> {};
template <typename Array>
auto constexpr cardinality_v = cardinality<Array>::value;
template <typename Array, size_t... Ns >
constexpr auto dimensions_impl(std::index_sequence<Ns...>) {
std::array<size_t, cardinality_v<Array>> result = { size_of_dim<Ns,Array>::value... };
return result;
}
template <typename Array>
constexpr auto dimensions() {
return dimensions_impl<Array>(std::make_index_sequence<cardinality_v<Array>>() );
}
int main()
{
auto test = [](auto arr){
constexpr auto dims = dimensions<decltype(arr)>();
for (auto d : dims)
std::cout << d << ", ";
std::cout << std::endl;
};
test(array1D<float, 1337>());
test(array2D<float, 7357, 17>());
test(array3D<float, 3, 4, 5>());
}
demo
相关文章:
- 初始化具有非默认构造函数的std::数组项的更好方法
- 输入std::数组时出现问题
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- C++如何生成std::数组列表
- 捕获lambda中的std::数组
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- std::数组边界检查如何工作?
- 通过unique_ptr访问std::数组
- 初始化 std::数组 of Eigen::Map
- 传递 std::数组变量作为参数
- 在带有模板的函数中传递未知大小的 std::数组.如何更正此代码?
- 如何将 std::find() 与 2d std: 数组一起使用?
- std::数组派生类聚合初始化
- 在std::数组内部使用时,无法从std::函数构造类
- 使用从1到N的数字初始化std数组模板参数
- 正在声明多维std::数组
- 使用std::index_sequence对std::数组初始化进行包扩展
- 使用 std::type T 的数组来构造可从 T 构造的 std::数组
- 从 std::数组创建十六进制代表
- 使用默认值初始化 std::数组