0

I saw some code like this:

template<unsigned N, unsigned M>  
bool compare (const char (&p1)[N], const char (&p2)[M])  
{  
    std::cout << "size : " << N << " " << M << std::endl;  
    return strcmp(p1, p2);  
} 

The template does not take typename or class as parameters, but using unsigned, what is the usage of using nontype parameter of template?

In which case we should write template using non type parameter?

ratzip
  • 1,571
  • 7
  • 28
  • 53
  • 1
    Please read some *introductory* book on C++. Select a book from [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).. whichever suits your taste and need. – Nawaz Nov 15 '16 at 16:07
  • I updated, basically I want to know when to use non type parameter template – ratzip Nov 15 '16 at 16:16
  • 1
    Whenever you want to write a series of functions or classes that vary in a way depending not on a type, but on a constant. In this case you will get a different function for each combination of array sizes. – Martin Bonner supports Monica Nov 15 '16 at 16:20
  • can not be a const variable passed to the functions? – ratzip Nov 15 '16 at 16:25
  • You have one use case in front of you: you can receive arrays of any size. – Guillaume Racicot Nov 15 '16 at 17:52

0 Answers0