Topic: templates' syntax


Author: allan_w@my-dejanews.com (Allan W)
Date: Thu, 20 Mar 2003 18:14:10 +0000 (UTC)
Raw View
FforeFverlearner@icFqmail.com ("Matan Nassau") wrote
> Why do I have to write template <class T> before each time I use the T
> identifier?

Only in definitions.

The same reason you must supply parameter names in a function
definition, even though you already supplied them in a template.

int foo(int a, int b); // The prototype
// After the prototype, the names "a" and "b" no longer mean anything.

//int foo(int,int) { return a+b; } // Invalid
int foo(int x, int y) { return x+y; } // Okay
// Note that the names "x" and "y" don't have to match the
// original prototyped names "a" and "b".

> template <class T>    //first time is understandable
> class foo
> {
>     T value;
> public:
>     T getValue()
> };

Starting here, the name "T" no longer means anything

> //template <class T>    //uncomment to resolve compiler error
> T foo<T>::getValue()
> {
>     return value;
> }

template<class C>
C foo<C>::getValue() { return value; }

It also matters with partial specialization:

template<class A, int B>
A foo<A,B>::getValue() { return value+B; }

> I don't understand why one definition of T to be a template for class foo is
> not enough.
>
> Matan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: FforeFverlearner@icFqmail.com ("Matan Nassau")
Date: Wed, 19 Mar 2003 19:52:22 +0000 (UTC)
Raw View
Why do I have to write template <class T> before each time I use the T
identifier?

template <class T>    //first time is understandable
class foo
{
    T value;
public:
    T getValue()
};

//template <class T>    //uncomment to resolve compiler error
T foo<T>::getValue()
{
    return value;
}

////
I don't understand why one definition of T to be a template for class foo is
not enough.

Matan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Thu, 20 Mar 2003 02:20:18 +0000 (UTC)
Raw View
Matan Nassau wrote:
> Why do I have to write template <class T> before each time I use the T
> identifier?

To say what it means at that point in the code.

>
> template <class T>    //first time is understandable
> class foo
> {
>     T value;
> public:
>     T getValue()
> };

Here, T has no meaning.  It was meaningful for the duration
of the definition of the class template foo above, but its
work is now done.

This is good; there could be many class templates in scope
at the same time, using T for any number of different purposes.
Restricting the scope of the name T in this way avoids
problems.

> //template <class T>    //uncomment to resolve compiler error
> T foo<T>::getValue()
> {
>     return value;
> }
>
> ////
> I don't understand why one definition of T to be a template for class foo is
> not enough.

Each definition is independent in its use of template
parameters.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]