1

I was working with code from an answer to this question:

#include <iostream>
#include <string>
using std::cin;
using std::cout;


template<int N, int I=N-1>
class Table : public Table<N, I-1>
{
public:
    static const int dummy;
};

template<int N>
class Table<N,0>
{
public:
    static int array[N];
    static const int dummy;
};

template<int N, int I>
const int Table<N,I>::dummy = Table<N,0>::array[I] = I + 0*Table<N,I-1>::dummy;

template<int N>
int Table<N,0>::array[N];

template<int N>
const int Table<N,0>::dummy = 0;

template class Table<5>;  //<-what does this statement do?  The program does not work without it.

int main()
{
    std::string s;
    int *array = Table<5,0>::array;
    
    for(int i = 0; i < 5; i++)
    {
        cout << array[i] << " ";
    }
    cout << "\n";
    
    cout << "Press ENTER to exit.\n";
    std::getline(cin, s);
}

The line template class Table<5>; just before the start of the main routine is marked off with a comment in the listing and shows a use of the work "template" that I am not familiar with. What is the justification for this line of code?

TRPh
  • 187
  • 1
  • 9
  • I don't see what part of the linked duplicate answers this question. As far as I can see, it only refers to moving implementation of templates to source files, and optimization. I didn't find anything about how it can change the behavior of the code shown. – François Andrieux Apr 01 '21 at 13:59
  • I added a more appropriate duplicate link. – François Andrieux Apr 01 '21 at 14:02

0 Answers0