T
Tony Johansson
Hello Experts!
What does this mean actually.
If you have a template with a type and non-type template argument, say, like
this
template<typename T, int a>
class Array {. . .};
then A<int, 1> and A<int, 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".
I mean that in main below I instansiate 10 objects using constant parameter
to the
class template which is not good.
So this will mean that I will create 10 different types because I create 10
objects.
We can also assume that the implamentation code is quite large in class
template Array.
At what situations will the object file be great on account of using
constant template parameter.
Give me some good example on this.
It's only the instansiated objects that really allocate memory.
Will it be big in this example with 10 objects?
If I instead had instansiated 100 object of this class template array
would the object file be big then.
I can't see why the object code would expand just because we have constant
template parameter. It's only when you instansiate object by value you
allocate memory which will cause the object file to expand.
int main()
{
Array<int,1> a1;
Array<int,2> a2;
Array<int,3> a3;
Array<int,4> a4;
Array<int,5> a5;
Array<int,6> a6;
Array<int,7> a7;
Array<int,8> a8;
Array<int,9> a9;
Array<int,10> a10;
return 0;
}
Many thanks
What does this mean actually.
If you have a template with a type and non-type template argument, say, like
this
template<typename T, int a>
class Array {. . .};
then A<int, 1> and A<int, 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".
I mean that in main below I instansiate 10 objects using constant parameter
to the
class template which is not good.
So this will mean that I will create 10 different types because I create 10
objects.
We can also assume that the implamentation code is quite large in class
template Array.
At what situations will the object file be great on account of using
constant template parameter.
Give me some good example on this.
It's only the instansiated objects that really allocate memory.
Will it be big in this example with 10 objects?
If I instead had instansiated 100 object of this class template array
would the object file be big then.
I can't see why the object code would expand just because we have constant
template parameter. It's only when you instansiate object by value you
allocate memory which will cause the object file to expand.
int main()
{
Array<int,1> a1;
Array<int,2> a2;
Array<int,3> a3;
Array<int,4> a4;
Array<int,5> a5;
Array<int,6> a6;
Array<int,7> a7;
Array<int,8> a8;
Array<int,9> a9;
Array<int,10> a10;
return 0;
}
Many thanks