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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With