What does this mean? Help please!

In my book it says:

float max(float, const int&);

This forces the creation of this version of the function template. It does not have much value in the case of our program example, but it can be useful when you know that several versions of a template function might be generated, but you want to force the generation of a subset that you plan to use, with arguments cast to the appropriate type where necessary.

How is it useful? I don't quite understand?
I think, it talks about synthesis of different functions of template according to the data types used.


so, if you have this:
template< class T > void max( T& , int& );

and in your program you do this:

1
2
3
max(int, int);
max(float, int);
max(double, int);


three functions would be generated, one for int, float and double.

It also says, if you want to force generation of subsets. This can be done using typecasting. But you should know what you are doing. something like this:

1
2
3
max((double)int, int);
max((double)float, int);
max(double, int);


only one function would be generated, i.e. for double only.

What does it mean by subsets? and can you explain the second part in a bit more detail please?

Thank You
thats what I said. instead of generating all the functions, you can just generate subsets by forcing the compiler by just typecasting the data types.

so instead of generating three functions, you are now generating only one function by typecasting the datat types.
Explicit template instantiations can be used in large code bases to speed up compilation and/or minimize code bloat. Briefly,

If we have a template that is only required to be used only with a few explicit types (in other words, a small subset of all possible types):

We can place the template declaration in a header file.

Place the template definition in a normal source file; and then explicitly instantiate the template for the subset of types for which the template is to be made available.
Last edited on
Topic archived. No new replies allowed.