Topic: Friends and templates


Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1999/06/02
Raw View
Hi,

in "The C++ Programming Language", 3rd edition, I've found the following
example (C.13.2, pp. 854-855; I've simplified it a bit):

template<class C> class Basic_ops {
 friend bool operator==(const C&, const C&);
};

template<class C> bool operator==(const C& a, const C& b) {...}

The text says that "a friend declared within a template is itself a
template". This seems to contradict the example in IS 14.5.3
[temp.friends]:

template<class T> task<T>* preempt(task<T>*);

template<class T> class task {
 ...
 friend void process(task<T>*);
 friend task<T>* preempt<T>(task<T>*);
 ...
};

Here, the text says that process is not a function template
specialization. Am I missing something or is C++PL really incorrect
here?

Then, the text in IS says "...because process does not have explicit
template-arguments...". Does this mean that process can have explicit
template-arguments even without prior declaration, i.e. would the code
be legal if I changed the declaration to

friend void process<T>(task<T>*);

The text "if the name of the friend is a ... template-id, the friend
declaration refers to a specialization of a function template" doesn't
seem to require that the function template is declared before the friend
declaration. Is this requirement implicit or is it not necessary to
declare process as a template in this case?

Next, is it correct that declaration 1 below refers to the preempt
template while declaration 2 declares a new non-template function?

friend task<T>* ::preempt(task<T>*); // 1
friend task<T>* preempt(task<T>*); // 2

Finally, the text under the example says that preempt refers to a
template "because ... preempt has an explicit template-argument <T>".
Would the code be still legal if I replaced <T> with <>?

Bye

Roman
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]