limitation of template parameters

T

Tony Johansson

Hello experts!

Is there any limitation for template parameter that normal function
parameters doesn't have?

What does this text below mean.
To use template parameters as constans is normally not a good ide because
each combination of parameters will the compiler consider as a new type. As
a result of this can the program expand in size.

//Tony
 
V

Victor Bazarov

Tony said:
Is there any limitation for template parameter that normal function
parameters doesn't have?

Yes, some conversions apply differently, some don't apply. Non-type
template arguments that are pointers or references need to have external
linkage (arguments to functions can be any pointers or references).
What does this text below mean.
To use template parameters as constans is normally not a good ide because
each combination of parameters will the compiler consider as a new type. As
a result of this can the program expand in size.

If you have a template with non-type template argument, say, like this

template<int a> class A {};

then A<1> and A<2> are different types. Now, if the A template had some
code (implementation), then if it's used, it would be _instantiated_ for
every template instantiation, which may lead to code "bloat".

Another example:

template<int a> double multiply1(double what) {
return a * what;
}

double multiply2(int what, double andwhat) {
return what * andwhat;
}

There is one version of 'multiply2' no matter how different its arguments
get, but there is as many versions of 'multiply1' as there are integer
constants used in your code.

V
 
T

Tony Johansson

I don't really understand this. I can accept and understand that the
compiler consider it as new types.
Can you explain it in aother way.
this A<1>, A<2>
If you have a template with non-type template argument, say, like this

template<int a> class A {};

then A<1> and A<2> are different types. Now, if the A template had some
code (implementation), then if it's used, it would be _instantiated_ for
every template instantiation, which may lead to code "bloat".

Whan do you actyally mean here?
Another example:
template<int a> double multiply1(double what) {
return a * what;
}

double multiply2(int what, double andwhat) {
return what * andwhat;
}

There is one version of 'multiply2' no matter how different its arguments
get, but there is as many versions of 'multiply1' as there are integer
constants used in your code.


//Tony
 
V

Victor Bazarov

Tony said:
I don't really understand this. I can accept and understand that the
compiler consider it as new types.
Can you explain it in aother way.
[...]

Nope... Get a book. And study. And then ask particular questions.
I ain't gonna rewrite the books that have been already written.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top