How to find total number of elements in a multidimensional array using std::extent. This is my sample code:
#include <iostream>
#include <type_traits>
int main()
{
    int32_t int_arr[10][100][1000];
    int32_t no_of_elements = 1;
    
    // compiler error in below for loop block
    for (int32_t i = 0; i < std::rank<decltype(int_arr)>{}; ++i)
        no_of_elements *= std::extent<decltype(int_arr), i>{};
        
    no_of_elements *= std::extent<decltype(int_arr), 0>{};
    no_of_elements *= std::extent<decltype(int_arr), 1>{};
    no_of_elements *= std::extent<decltype(int_arr), 2>{};
    std::cout << no_of_elements << std::endl;
    std::cout << sizeof(int_arr) / sizeof(std::remove_all_extents<decltype(int_arr)>::type) << std::endl;
        
    return 0;
}
error: the value of ‘i’ is not usable in a constant expression 9 | no_of_elements *= std::extent<decltype(int_arr), i>{};
Aucun commentaire:
Enregistrer un commentaire