在编译时通过 constexpr 或模板函数获取多维 std::array 的大小
Get the size of multi dimensional std::array at compile time via constexpr or template function
我使用三维std::array
,因为大小在编译时已经知道了。但是,我注意到 size(( 函数不是静态的,因此无法访问 constexpr/template 函数。
我已经找到了下面的演示示例,它估计了一维std::array
的大小。但是,这不适用于两个或多个维度。有没有办法通过编写一个函数来返回其他维度,该函数具有x, y, z, ..
维度的附加模板参数dim
?
// 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<class T, std::size_t N>
auto array_size_helper(const array1D<T, N>&) -> std::integral_constant<std::size_t, N>;
template<class Array>
using array_size = decltype(array_size_helper(std::declval<const Array&>()));
template<class Array>
constexpr auto static_size() -> decltype(array_size<Array>::value) {
return array_size<Array>::value;
}
template<class Array>
constexpr auto static_size(Array const&) -> decltype(static_size<Array>()) {
return static_size<Array>();
}
int main()
{
std::cout << static_size<array3D<float, 3, 4, 5>>();
}
对于一维情况,您也可以使用为std::array
定义的std::tuple_size
:
int main()
{
std::cout << std::tuple_size<array3D<float, 3, 4, 5>>();
}
关于您的实际问题。如果我理解正确,您希望在大小函数上有一个附加参数,您可以使用该参数选择应返回大小的维度,对吗?
这可以通过使用递归轻松完成。这是一个工作示例:
// 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> {};
int main()
{
std::cout << size_of_dim<0,array3D<float, 3, 4, 5>>() << std::endl;
std::cout << size_of_dim<1,array3D<float, 3, 4, 5>>() << std::endl;
std::cout << size_of_dim<2,array3D<float, 3, 4, 5>>() << std::endl;
}
演示
相关文章:
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 标准::unordered_map 中的 std::array 的值初始化
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何读/写或遍历 std::array 中的特定元素范围?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?
- 将 std::array 推回 std::vector N 次的优雅方式
- 如何在 c++11 中静态断言 std::array 类成员进行排序?
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上
- 按值返回 std::array
- 如何将变量内容常量转换为 std::array 的大小?
- 将初始化器列表/聚合初始化转发到 std::array 成员
- 无法推断类中的类型,构造函数采用 std::array
- 为什么 char 可以在 std::array 中初始化为 nullptr,而不是单独初始化?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问