template<int __inst> ...

Discussion in 'C++' started by David Lindauer, Sep 10, 2004.

  1. 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
    David Lindauer, Sep 10, 2004
    #1
    1. Advertising

  2. "David Lindauer" <> wrote in message
    news:...
    > 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
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Sep 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    452
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    945
    Jim Langston
    Oct 20, 2005
  5. Steve Hicks
    Replies:
    2
    Views:
    1,263
Loading...

Share This Page