Topic: Friend functions of class templates


Author: Gregory Bond <gnb@itga.com.au>
Date: 1998/06/26
Raw View
I'm confused.

The Dec 96 CD2 has (in section 14.5.3 templ.friend) the following
example:
[Example:
          template<class T> class task {
              // ...
              friend void next_time();
              friend task<T>* preempt(task<T>*);
              friend task* prmt(task*);           // task is task<T>
              friend class task<int>;
              // ...
          };
  Here,  next_time()  and  task<int> become friends of all task classes,
  and each task has appropriately typed functions preempt()  and  prmt()
  as  friends.   The preempt functions might be defined as a template as
  follows
          template<class T> task<T>* preempt(task<T>* t) { /* ... */ }
   --end example]

However when I attempt to compile this with egcs1.0.1 it says:

tt.C:4: warning: friend declaration `class task<T> * preempt(class task<T> *)'
tt.C:4: warning:   will not be treated as a template instantiation
tt.C:4: warning:   unless you compile with -fguiding-decls
tt.C:4: warning:   or add <> after the function name
tt.C:5: warning: friend declaration `class task<T> * prmt(class task<T> *)'
tt.C:5: warning:   will not be treated as a template instantiation

and indeed the egcs FAQ says

>>Friend Templates
>>In order to make a specialization of a template function a friend of a
>>(possibly template) class, you must explicitly state that the friend
>>function is a template, by appending angle brackets to its name, and
>>this template function must have been declared already. An error in
>>the last public comment draft of the ANSI/ISO C++ Standard has led
>>people to believe that was not necessary, but it is, and it was fixed
>>in the final version of the Standard.

If this is the case, what should the task example above look like?

Greg
--
Gregory Bond                            ITG Australia Ltd, Melbourne, Australia
<mailto:gnb@itga.com.au>                           <http://www.bby.com.au/~gnb>
From: bruce@itga.com.au (Do not use this address.  It catches junk email.)
From: bruce@bby.com.au, bruce@melba.bby.com.au  (So do these ones)
---
[ 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              ]