template<int __inst> ...

D

David Lindauer

so I see declarations for various template classes and functions which
start with:

template <int __inst>

unfortunately the compiler I use doesn't allow that so I can't
experiment... but I am guessing from the text that what is expected is
an integer value. I assume that that integer is used to further qualify
the template instantiation, so that there could be multiple
instantiations for the template that were unrelated to a change in type.

for example:

template <int __inst>
void myfunc( char *a)
{
....
}

we could have a myfunc<0> and a myfunc<1> and so forth...

I'm somewhat fresh on templates so if someone could clarify that this is
indeed what is the case I'd appreciate it. If not I would appreciate
some clarification on what exactly the intent of this is.

In any case I would also like to hear why one would want to use this
feature? If it helps the code I am reviewing is the SGI template
library...

Thanks,

David
 
I

Ivan Vecerina

David Lindauer said:
so I see declarations for various template classes and functions which
start with:

template <int __inst>

unfortunately the compiler I use doesn't allow that so I can't
experiment... but I am guessing from the text that what is expected is
an integer value. I assume that that integer is used to further qualify
the template instantiation, so that there could be multiple
instantiations for the template that were unrelated to a change in type.

It's pretty much it. But why don't your read some book or tutorial
about templates?
For example, Thinking in C++ (Vol 1 chap 16, and Vol 2 chap 3),
has a decent intro, and can be downloaded here:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Also, BTW, there are many free C++ compilers that support templates
(gcc, versions from Microsoft or Borland, and many more).
In any case I would also like to hear why one would want to use this
feature? If it helps the code I am reviewing is the SGI template
library...
There are too many different possible uses to mention.
A classic starter might be:
#include <iostream>

template<int i> struct factorial
{ enum { value=i*factorial<i-1>::value }; };

// specialize the function to end recursion
template<> struct factorial<1> { enum { value = 1 }; };

int main()
{
// the factorial value is computed at compile-time.
std::cout << factorial<5>::value << std::endl;
}

A more practical example could be a small vector linear
algebra library that uses the following definition of
a vector type:
template<int N>
struct vect { double m[N]; }

hth,
Ivan
 

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
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top