通过constexpr或模板函数在编译时获取多维std :: array的大小 [英] Get the size of multi dimensional std::array at compile time via constexpr or template function
问题描述
我使用三维 std :: array
,因为在编译时已经知道大小。但是,我注意到size()函数不是静态的,因此constexpr / template函数无法访问。
I use a three dimensional std::array
, because the size is already known at compile. However, I noticed that the size() function is not static, and thus, inaccessible for constexpr/template functions.
我已经找到下面的演示示例,该示例估计一维 std :: array
的大小。但是,这不适用于两个或多个维度。有没有一种方法可以通过为 x,y,z,...编写带有附加模板参数
dim
的函数来返回其他维度。 code>维?
I already found the demo example below, which estimates the size of a one dimensional std::array
. However, this does not work for two or more dimensions. Is there a way to return the other dimensions by writing a function with an additional template parameter dim
for the x, y, z, ..
dimension?
// 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
:
For the one dimensional case, you could use std::tuple_size
which is defined for std::array
as well:
int main()
{
std::cout << std::tuple_size<array3D<float, 3, 4, 5>>();
}
关于您的实际问题。如果我理解的正确,那么您希望在size函数上有一个附加参数,您可以通过该参数选择要返回尺寸的尺寸,对吗?
Regarding your actual problem. If I understand you right you want to have an additional parameter on your size function with which you can select the dimension for which the size should be returned, right?
这可以使用递归可以轻松完成。这是一个工作示例:
This can be done easily by using recursion. Here is a working example:
// 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;
}
这篇关于通过constexpr或模板函数在编译时获取多维std :: array的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!