Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the argument list in variadic templates for n-dimensional array

I have a template class with the following specification:

template <typename T, size_t... Dims> class Array;

And say it can be used as follows:

// Define a 2X3X4 array of integers.  Elements are uninitialized.
Array<int, 2, 3, 4> a, b;
Array<short, 2, 3, 4> c;
Array<int, 0> e1; // This line must cause a compile-time error.

How can I achieve this functionality? I thought if I could extract all the argument list I could then create the n-dimentional array as a straight forward recursive call. How can I do it now

like image 992
footy Avatar asked Dec 01 '25 00:12

footy


1 Answers

Create a specialization for it:

#include <iostream>

template <typename T, std::size_t... Dims>
struct Array {};

template <typename T>
struct Array<T, 0>; // leave as an incomplete type

int main()
{
    Array<int, 3> x; // OK
    Array<int, 0> y; // error: aggregate ‘Array<int, 0u> y’ has incomplete type and cannot be defined
}
like image 105
David G Avatar answered Dec 02 '25 15:12

David G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!