Find Dimension of Array

  c++, templates

I am trying to count the # of dimension:

template <class T>
struct dimension {
    static constexpr auto n = 0;
    void print() {
        std::cout << "T is not an array: " << __PRETTY_FUNCTION__ << std::endl;
    }
};

template <class T, size_t N>
struct dimension<T(&)[N]> {
    static constexpr auto n = 1 + dimension<T>::n;
    void print() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        dimension<T> d;
        d.print();
    }
};


int main() {
    int x[6][3];
    dimension<decltype((x))> d;
    d.print();
}

And the output:

void dimension<T (&)[N]>::print() [with T = int [3]; long unsigned int N = 6]
T is not an array: void dimension<T>::print() [with T = int [3]]

However, for some reason, when dimension<T>::n in the partial specialized template, it actually instantiate dimension<int[3]> instead of dimension<int, 3>. What did I do wrong?

Thanks!

Source: Windows Questions C++

LEAVE A COMMENT