Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a second specified type mean in a template typename declaration?

Tags:

c++

templates

I am comfortable with templating functions and classes but I didn't know what to make of this when I saw it. I am sure its probably everyday syntax to most but I would like to get a well clarified explanation if anyone has one for me. What does the second uint32-t max mean and how is it used in the templated type?

Heres the syntax:

template <typename T, uint32_t max>

Thanks in advance.

like image 266
hally9k Avatar asked Nov 27 '22 00:11

hally9k


1 Answers

It's a second parameter to the template. And template parameters don't have to be types. They can be constants or templates as well. Thus, given

template <typename T, uint32_t max> class TC {};

you would instantiate it:

TC< MyClass, 42 > t;

(for example.) Similarly, if it is a function template:

template <typename T, uint32_t max> void tf( T (&array)[max] );

type deduction can be used to determine the (numeric) value of max.

Such value templates cannot have just any type; it must be an integral type or a pointer or reference.

like image 190
James Kanze Avatar answered Dec 19 '22 04:12

James Kanze