Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过constexpr或template函数在编译时获取多维std::array的大小_C++_C++11_Templates_Template Meta Programming_Constexpr - Fatal编程技术网

C++ 通过constexpr或template函数在编译时获取多维std::array的大小

C++ 通过constexpr或template函数在编译时获取多维std::array的大小,c++,c++11,templates,template-meta-programming,constexpr,C++,C++11,Templates,Template Meta Programming,Constexpr,我使用一个三维的std::array,因为编译时已经知道了大小。但是,我注意到size()函数不是静态的,因此对于constexpr/template函数是不可访问的 我已经找到了下面的演示示例,它估计了一维std::array的大小。但是,这不适用于两个或更多维度。是否有方法通过为x,y,z,…维度编写一个带有附加模板参数dim的函数来返回其他维度 // Example program #include <iostream> #include <string> #inc

我使用一个三维的
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>>();
}
//示例程序
#包括
#包括
#包括
//某些容器类的typedef
模板
使用array1D=std::array;
模板
使用array2D=std::array;
模板
使用array3D=std::array;
模板
自动数组大小辅助对象(常量数组1d&->std::整数常量;
模板
使用array_size=decltype(array_size_helper(std::declval());
模板
constexpr auto static_size()->decltype(数组大小::值){
返回数组大小::值;
}
模板
constexpr auto static_size(数组const&)->decltype(static_size()){
返回静态_size();
}
int main()
{

std::cout对于一维情况,您可以使用
std::tuple_size
,它也是为
std::array
定义的:

int main()
{
    std::cout << std::tuple_size<array3D<float, 3, 4, 5>>();
}
intmain()
{

std::在您的示例中,如果使用
array3D
,大小会是多少?
3
4
5
?或
3*4*5
?您提供的是恒定的大小。为什么您不能将它们各自的大小相乘,然后得到总大小?在我的示例中,我只得到X维:3。其他两个不包括在内:/这是一个非常复杂的问题很好的解决方案。
// 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;
}