I need to "pack" and "unpack" the dimensions of a multidimensional C-style array at compile time. By "pack," I mean given a type which represents an array's dimensions such as
template<typename T, size_t N2, size_t N3> struct SomeType<T,0,0,N2,N3> { ... };
that I can extract a type T**[N2][N3]
and perform the converse operation so that given a type T**[N2][N3]
, I can populate an std::index_sequence
with parameters <0,0,N2,N3>
I've been able to perform the "pack" operation with the code:
/* pack_array_type<T,N0> is T[N0]
* pack_array_type<T,0> is T*
* pack_array_type<T,N0,N1> is T[N0][N1]
* pack_array_type<T,0,N1> is T*[N1]
* pack_array_type<T,0,0> is T**
* etc */
using namespace std;
template<typename,typename> struct pack_array_type_impl;
template<typename T, size_t N0, size_t...Ns>
struct pack_array_type_impl<T,index_sequence<N0,Ns...>> {
using type = typename
pack_array_type_impl<T[N0],index_sequence<Ns...>>::type;
};
template<typename T, size_t...Ns>
struct pack_array_type_impl<T,index_sequence<0,Ns...>> {
using type = typename
pack_array_type_impl<T*,index_sequence<Ns...>>::type;
};
template<typename T, size_t N>
struct pack_array_type_impl<T,index_sequence<N>> { using type = T[N];};
template<typename T>
struct pack_array_type_impl<T,index_sequence<0>> { using type = T*; };
template<typename T, size_t...N>
using pack_array_type = typename
pack_array_type_impl<T,index_sequence<N...>>::type;
My attempt at the inverse operation to "unpack" the dimensions is
template<typename, typename> struct unpacked_array_type_impl;
template<typename T, size_t...I>
struct unpacked_array_type_impl<T,index_sequence<I...>> {
using index_type = index_sequence<extent<T[I]>::value...>;
};
template<typename T>
struct unpacked_array_type {
using value_type = decay_t<T>;
// EDIT: Need condition here to prevent infinite recursion
using index_type =
typename unpacked_array_type_impl<T,make_index_sequence<rank<T>::value>>::index_type;
}
// Print indices for testing
template<size_t...N>
void print_sequence( const index_sequence<N...>& seq, ostream& os ) {
using expand_type = int[];
os << "[";
(void) expand_type
{ 0, ((void)(os << integral_constant<size_t,N>() << " ") , 0) ... };
os << "]\n";
}
int main( int argc, char* argv[] ) {
typename unpacked_array_type<double**[3][4][5]>::index_type x;
// print_sequence(x,cout); // Desired output = [0 0 3 4 5 ]
return 0;
}
` Compiling with clang 5.0.1, gives the error
main.cpp:54:12: error: implicit instantiation of undefined template 'unpacked_array_type_impl<double **[3][4][5], details::make_index_sequence<3> >' typename unpacked_array_type_impl<T,make_index_sequence<rank<T>::value>>::type; ^ main.cpp:68:12: note: in instantiation of template class 'unpacked_array_type<double**[3][4][5]>' requested here typename unpacked_array_type<double**[3][4][5]>::type x; ^ main.cpp:43:37: note: template is declared here template<typename, typename> struct unpacked_array_type_impl; ^`
Is it possible to extract these dimensions?
Note: I am using my own implementation of some C++14 features such as std::index_sequence
in C++11.
EDIT: Added code https://coliru.stacked-crooked.com/a/6776152e348c2c57 This builds, but outputs [3 4 5 ] instead of [0 0 3 4 5 ]
Some errors in your code.
(1) use ever type
or index_type
but not ask for type
// .......................................................................vvvv
typename unpacked_array_type_impl<T,make_index_sequence<rank<T>::value>>::type;
when the class define index_type
template<typename T, size_t...I>
struct unpacked_array_type_impl<T,index_sequence<I...>> {
using index_type = index_sequence<extent<T[I]>::value...>;
}; // ..^^^^^^^^^^
Same problem for unpacked_array_sequence
that define a index_type
when in main()
you ask for type
(2) use std::extent<T, I>::value...
, not extent<T[I]>::value...
.
Also observe that with extent<T[I]>::value ...
you define T[0]
(when I
is zero) that isn't acceptable in standard C++.
(3) add template <std::size_t ... N>
before
void print_sequence( const index_sequence<N...>& seq, ostream& os ) {
Correcting this errors you should get the output
[3 4 5 ]
that isn't exactly what you asked but is an improvement.
To get
[0 0 3 4 5 ]
I propose to rewrite unpacked_array_type_impl
as follows
template <typename, typename IS = std::index_sequence<>>
struct unpacked_array_type_impl
{ using type = IS; };
template <typename T, std::size_t ... I>
struct unpacked_array_type_impl<T*, std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<0u, I...>>
{ };
template <typename T, std::size_t N, std::size_t ... I>
struct unpacked_array_type_impl<T[N], std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<I..., N>>
{ };
and use it as follows
template<typename T>
struct unpacked_array_type
{ using type = typename unpacked_array_type_impl<T>::type; };
The following is a full working example
#include <utility>
#include <iostream>
#include <type_traits>
template <typename, typename IS = std::index_sequence<>>
struct unpacked_array_type_impl
{ using type = IS; };
template <typename T, std::size_t ... I>
struct unpacked_array_type_impl<T*, std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<0u, I...>>
{ };
template <typename T, std::size_t N, std::size_t ... I>
struct unpacked_array_type_impl<T[N], std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<I..., N>>
{ };
template<typename T>
struct unpacked_array_type
{ using type = typename unpacked_array_type_impl<T>::type; };
// Print indices for testing
template <std::size_t ... N>
void print_sequence (std::index_sequence<N...> const & seq,
std::ostream & os)
{
using expand_type = int[];
os << "[";
(void) expand_type { 0, ((void)(os << N << " ") , 0) ... };
os << "]\n";
}
int main ()
{
typename unpacked_array_type<double**[3][4][5]>::type x;
print_sequence(x, std::cout); // Desired output = [0 0 3 4 5 ]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.