Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template<int __inst> ...

Reply
Thread Tools

template<int __inst> ...

 
 
David Lindauer
Guest
Posts: n/a
 
      09-10-2004
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
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-10-2004
"David Lindauer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/...ngInCPP2e.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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments