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.
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>>();
}
Aucun commentaire:
Enregistrer un commentaire