public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* friend templates in gcc-2.8.0 & egcs-1.0.1
@ 1998-01-29 16:16 Levente Farkas
  1998-01-30 17:54 ` Alexandre Oliva
  0 siblings, 1 reply; 7+ messages in thread
From: Levente Farkas @ 1998-01-29 16:16 UTC (permalink / raw)
  To: egcs

hi,
[may be this's not the proper place for my question, but...]
I'm just upgrade to egcs-1.0.1 and try to recompile my project, which
contain the following segment of code for numeric vector in n_vector.h :
---------------------
template <class T>
class N_Vector : public Vector<T>
{
  public :
  ...
 N_Vector<T>   operator * (const T &) const;
 friend N_Vector<T> operator *(const T &, const N_Vector<T> &);
}
 ...
template <class T>
inline N_Vector<T> 
operator *(const T & Scalar, const N_Vector<T> & V)
{
  return V * Scalar;
}
--------------------
and there is templates.cc for in instantiation with :
--------------------
...
template inline N_Vector<double> operator *(const double &, const
N_Vector<double> &);
--------------------
this is work since gcc 2.6.x- 2.7.2 (-fno-implicit-templates) compiled, 
but after the upgrade to egcs-1.0.1 I've got the following warning during
compilation :
--------------------
n_vector.h:69: warning: friend declaration `class N_Vector<T> operator *(const
T &, const class N_Vector<T> &)'
n_vector.h:69: warning:   will not be treated as a template instantiation
n_vector.h:69: warning:   unless you compile with -fguiding-decls
n_vector.h:69: warning:   or add <> after the function name
--------------------
but no error, but at link-time (many similar) :
--------------------
impedance.o:/home/lfarkas/ssa/impedance.cc:617: more undefined references to
`operator*(double const &, 
N_Vector<double> const &)' follow
collect2: ld returned 1 exit status
make: *** [ssa] Error 1
--------------------

so my question what is the problem ? and what's the solution ?
this is a quote from  1997 C++  Standard Public Review :
--------------------
 14.5.3  Friends                                          [temp.friend]

1 A friend function of a class template can be a function template or an
  ordinary (non-template) function.  [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) { /* ... */ }
--------------------

ps. please replay to my private address too.

-- Levente

---------------------------------------------------------------------
E-Mail:   Levente Farkas <!spam-lfarkas@ohsh.u-szeged.hu>
Homepage: http://www.inf.u-szeged.hu/~lfarkas/
PGP public key & Geek Code: !spam-lfarkas@anna.inf.u-szeged.hu
This message was sent by XF-Mail, Date: 29-Jan-98  Time: 17:01:04
---------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-29 16:16 friend templates in gcc-2.8.0 & egcs-1.0.1 Levente Farkas
@ 1998-01-30 17:54 ` Alexandre Oliva
  1998-01-31  2:48   ` Levente Farkas
  1998-02-08  0:14   ` Jeffrey A Law
  0 siblings, 2 replies; 7+ messages in thread
From: Alexandre Oliva @ 1998-01-30 17:54 UTC (permalink / raw)
  To: Levente Farkas; +Cc: egcs

This should be in the FAQ list.  Jeff, would you put this in?

Levente Farkas writes:

> n_vector.h:69: warning: friend declaration `class N_Vector<T> operator *(const
> T &, const class N_Vector<T> &)'
> n_vector.h:69: warning:   will not be treated as a template instantiation
> n_vector.h:69: warning:   unless you compile with -fguiding-decls
> n_vector.h:69: warning:   or add <> after the function name

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.

Which means you must declare operator* as a template function, before
it is referred to, but then you must have declared the class template
too, as follows:

template <class T> class N_Vector;
template <class T> inline N_Vector<T> operator*(const T&, const N_Vector<T>&);

Now you may define N_Vector:

> template <class T>
> class N_Vector : public Vector<T>
> {
>   public :
>   ...
>  N_Vector<T>   operator * (const T &) const;
but you must add angle brackets when you refer to a template function:
>  //friend N_Vector<T> operator *
   friend N_Vector<T> operator * <>
>  (const T &, const N_Vector<T> &);
> }
>  ...

and then you can define the template function:

> template <class T>
> inline N_Vector<T> 
> operator *(const T & Scalar, const N_Vector<T> & V)
> {
>   return V * Scalar;
> }
> --------------------

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-30 17:54 ` Alexandre Oliva
@ 1998-01-31  2:48   ` Levente Farkas
  1998-01-31 12:16     ` Alexandre Oliva
  1998-01-31 13:02     ` Mumit Khan
  1998-02-08  0:14   ` Jeffrey A Law
  1 sibling, 2 replies; 7+ messages in thread
From: Levente Farkas @ 1998-01-31  2:48 UTC (permalink / raw)
  To: Alexandre Oliva, egcs

On 31-Jan-98 Alexandre Oliva wrote:
> This should be in the FAQ list.  Jeff, would you put this in?
> 
> Levente Farkas writes:
> 
>> n_vector.h:69: warning: friend declaration `class N_Vector<T> operator
>> *(const
>> T &, const class N_Vector<T> &)'
>> n_vector.h:69: warning:   will not be treated as a template instantiation
>> n_vector.h:69: warning:   unless you compile with -fguiding-decls
>> n_vector.h:69: warning:   or add <> after the function name
> 
> 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.
> 
> Which means you must declare operator* as a template function, before
> it is referred to, but then you must have declared the class template
> too, as follows:
> 
> template <class T> class N_Vector;
> template <class T> inline N_Vector<T> operator*(const T&, const
> N_Vector<T>&);
> 
> Now you may define N_Vector:
> 
>> template <class T>
>> class N_Vector : public Vector<T>
>> {
>>   public :
>>   ...
>>  N_Vector<T>   operator * (const T &) const;
> but you must add angle brackets when you refer to a template function:
>>  //friend N_Vector<T> operator *
>    friend N_Vector<T> operator * <>
>>  (const T &, const N_Vector<T> &);
>> }
>>  ...
> 
> and then you can define the template function:
> 
>> template <class T>
>> inline N_Vector<T> 
>> operator *(const T & Scalar, const N_Vector<T> & V)
>> {
>>   return V * Scalar;
>> }
>> --------------------

thanks for it, but
1, from where can I know things like that ?
2, if I modify the program this way, than it's no longer works on
all other unix system (Solaris, AIX) even with gcc (but "older" version
2.7.2) and I'm not the sysadm everywhere.

So is there any backward compatible solution ???
or I can't use egcs :-(
or redesign our project (in this case friend is unnecessary, but..):-(

-- Levente

---------------------------------------------------------------------
E-Mail:   Levente Farkas <!spam-lfarkas@ohsh.u-szeged.hu>
Homepage: http://www.inf.u-szeged.hu/~lfarkas/
PGP public key & Geek Code: !spam-lfarkas@anna.inf.u-szeged.hu
This message was sent by XF-Mail, Date: 31-Jan-98  Time: 11:31:58
---------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-31  2:48   ` Levente Farkas
@ 1998-01-31 12:16     ` Alexandre Oliva
  1998-01-31 13:14       ` Mumit Khan
  1998-01-31 13:02     ` Mumit Khan
  1 sibling, 1 reply; 7+ messages in thread
From: Alexandre Oliva @ 1998-01-31 12:16 UTC (permalink / raw)
  To: Levente Farkas; +Cc: egcs

Levente Farkas writes:

> 1, from where can I know things like that ?

It is Standard C++, but I'm not sure there are any books that cover
this already.  You might buy a copy of the C++ Standard :-)

But I'd rather have this particular issue in the FAQ.

> So is there any backward compatible solution ???

You may use -fguiding-decls to have a backward-compatible behavior,
but have in mind that you'll not be programming standard C++.  The
best you can do is to use SGI STL-like compatibility macros.  Take a
look at stl_config.h, as installed by egcs and gcc, and modify your
code so that it will compile with both outdated compilers and
Standard-compliant ones.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-31  2:48   ` Levente Farkas
  1998-01-31 12:16     ` Alexandre Oliva
@ 1998-01-31 13:02     ` Mumit Khan
  1 sibling, 0 replies; 7+ messages in thread
From: Mumit Khan @ 1998-01-31 13:02 UTC (permalink / raw)
  To: Levente Farkas; +Cc: egcs

On Sat, 31 Jan 1998, Levente Farkas wrote:

> 2, if I modify the program this way, than it's no longer works on
> all other unix system (Solaris, AIX) even with gcc (but "older" version
> 2.7.2) and I'm not the sysadm everywhere.
> 
> So is there any backward compatible solution ???
> or I can't use egcs :-(
> or redesign our project (in this case friend is unnecessary, but..):-(
> 

The way I do it is to test for the new friend template decl feature using
an autoconf macro, and simply use ifdefs in the code. It's quite simple
really, and definitely not a roadblock for even large bodies of code.

For an example of how to do this, download my changes for octave-2.0.9
(it's a GNU matlab-like program developed by John Eaton) from
  
  ftp://ftp.xraylith.wisc.edu/pub/khan/gnu-win32/cygwin32/ports/

It has the autoconf macro and shows how you can conditionalize your
code. 

Mumit



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-31 12:16     ` Alexandre Oliva
@ 1998-01-31 13:14       ` Mumit Khan
  0 siblings, 0 replies; 7+ messages in thread
From: Mumit Khan @ 1998-01-31 13:14 UTC (permalink / raw)
  To: egcs

On 31 Jan 1998, Alexandre Oliva wrote:

> You may use -fguiding-decls to have a backward-compatible behavior,
> but have in mind that you'll not be programming standard C++.  The
> best you can do is to use SGI STL-like compatibility macros.  Take a
> look at stl_config.h, as installed by egcs and gcc, and modify your
> code so that it will compile with both outdated compilers and
> Standard-compliant ones.
> 

-fguiding-decls, IMO, should not be recommended since then people
have more problems than they started out with (mostly standard library
related ones -- you end up with undefined externals and so on). 

Autoconf is the answer. I used the attached macro I use in aclocal.m4.

Regards, 
Mumit -- khan@xraylith.wisc.edu
http://www.xraylith.wisc.edu/~khan/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: friend templates in gcc-2.8.0 & egcs-1.0.1
  1998-01-30 17:54 ` Alexandre Oliva
  1998-01-31  2:48   ` Levente Farkas
@ 1998-02-08  0:14   ` Jeffrey A Law
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey A Law @ 1998-02-08  0:14 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: Levente Farkas, egcs

  In message <orhg6l5s7t.fsf@amazonas.dcc.unicamp.br>you write:
  > This should be in the FAQ list.  Jeff, would you put this in?
[ ... ]
  > 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.
Done.

jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1998-02-08  0:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-29 16:16 friend templates in gcc-2.8.0 & egcs-1.0.1 Levente Farkas
1998-01-30 17:54 ` Alexandre Oliva
1998-01-31  2:48   ` Levente Farkas
1998-01-31 12:16     ` Alexandre Oliva
1998-01-31 13:14       ` Mumit Khan
1998-01-31 13:02     ` Mumit Khan
1998-02-08  0:14   ` Jeffrey A Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).