Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect C-style multidimensional arrays in templates specialization?

I have the following code:

enum type_kind{unkown=-1,carray, multi_carray};

template<class T>
struct detect_carray{
   constexpr static int kind=unkown;
};
template<class T, std::size_t N>
struct detect_carray<T[N]>{
   constexpr static int kind=carray;
};

Now, I want to add another specialization for detecting multidimensional arrays in C-style, that is, T[a][b]....

What is the syntax to achieve this? Can I use Variadic templates?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I expect the following behavior:

int main()
{
std::cout<<detect_carray<std::vector<int>>::kind;//-1
std::cout<<detect_carray<int[3]>::kind;//0
std::cout<<detect_carray<double[3][5]>::kind;//1
std::cout<<detect_carray<std::complex<double>[3][5][8][16]>::kind;//1
//Correct out: -1011
}

>Solution :

Just add a specialization for multidimensional arrays:

template<class T, std::size_t N1, std::size_t N2>
struct detect_carray<T[N1][N2]>{
   constexpr static int kind=multi_carray;
};

then

std::cout<<detect_carray<std::vector<int>>::kind;//-1
std::cout<<detect_carray<int[3]>::kind;//0
std::cout<<detect_carray<double[3][5]>::kind;//1
std::cout<<detect_carray<std::complex<double>[3][5][8][16]>::kind;//1

LIVE

BTW: For double[3][5], T will be double (and N1 will be 3 and N2 will be 5). For std::complex<double>[3][5][8][16], T will be std::complex<double> [8][16] (and N1 will be 3 and N2 will be 5).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading