public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Template mangling bug in egcs-2.90.16
@ 1997-11-17  2:12 Andreas Schwab
  1997-11-17  8:12 ` Tom Kunert
       [not found] ` <Pine.LNX.3.96.971117163642.10130A-100000.cygnus.egcs@rp168.urz.tu-dresden.de>
  0 siblings, 2 replies; 7+ messages in thread
From: Andreas Schwab @ 1997-11-17  2:12 UTC (permalink / raw)
  To: egcs

In the following example the function f1 is referenced under the name
"f1(int)", but it should be "A<int> f1<int>(int)".

$ cat template.cc
template <class X>
struct A
{
  X x;
  friend A f1 (X);
};

template <class X> A<X> f1 (X) { A<X> a; return a; }
template <class X> A<X> f2 (const A<X>& a) { return f1 (a.x); }

template A<int> f1 (int);
template A<int> f2 (const A<int>&);
$ gcc -c template.cc
$ nm -C template.o
00000000 ? __FRAME_BEGIN__
         U f1(int)
00000000 T A<int> f1<int>(int)
00000012 T A<int> f2<int>(A<int> const &)
00000000 t gcc2_compiled.

-- 
Andreas Schwab                                      "And now for something
schwab@issan.informatik.uni-dortmund.de              completely different"
schwab@gnu.org

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

* Re: Template mangling bug in egcs-2.90.16
  1997-11-17  2:12 Template mangling bug in egcs-2.90.16 Andreas Schwab
@ 1997-11-17  8:12 ` Tom Kunert
  1997-11-17 12:30   ` Alexandre Oliva
       [not found]   ` <or67prihnu.fsf.cygnus.egcs@grupiara.dcc.unicamp.br>
       [not found] ` <Pine.LNX.3.96.971117163642.10130A-100000.cygnus.egcs@rp168.urz.tu-dresden.de>
  1 sibling, 2 replies; 7+ messages in thread
From: Tom Kunert @ 1997-11-17  8:12 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: egcs

On 17 Nov 1997, Andreas Schwab wrote:

> In the following example the function f1 is referenced under the name
> "f1(int)", but it should be "A<int> f1<int>(int)".
> 
> $ cat template.cc
> template <class X>
> struct A
> {
>   X x;
>   friend A f1 (X);
> };
> 
> template <class X> A<X> f1 (X) { A<X> a; return a; }
> template <class X> A<X> f2 (const A<X>& a) { return f1 (a.x); }
> 
> template A<int> f1 (int);
> template A<int> f2 (const A<int>&);
> $ gcc -c template.cc
> $ nm -C template.o
> 00000000 ? __FRAME_BEGIN__
>          U f1(int)
> 00000000 T A<int> f1<int>(int)
> 00000012 T A<int> f2<int>(A<int> const &)
> 00000000 t gcc2_compiled.

Obviously the compiler sees two different functions f1(1) : One is the
instantiation of a template and one is an ordinary function. According
to the DWP these functions can coexist, the overload-resolution rules
prefer the normal function. 
The friend declaration in the example is regarded as function, not as
template, so it is undefined (U). Any call to f1(int) will result in a
linker error: f1(int) not defined.
The question is, whether the compiler is allowed to regard the friend
declaration as an ordinary function. I think, this should be a function
template and there is a bug in egcs. (See DWP Nov. 96, 14.3.5)

Thomas Kunert <tom@rp168.urz.tu-dresden.de>



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

* Re: Template mangling bug in egcs-2.90.16
  1997-11-17  8:12 ` Tom Kunert
@ 1997-11-17 12:30   ` Alexandre Oliva
       [not found]   ` <or67prihnu.fsf.cygnus.egcs@grupiara.dcc.unicamp.br>
  1 sibling, 0 replies; 7+ messages in thread
From: Alexandre Oliva @ 1997-11-17 12:30 UTC (permalink / raw)
  To: Tom Kunert; +Cc: Andreas Schwab, egcs

Tom Kunert writes:

> On 17 Nov 1997, Andreas Schwab wrote:

>> In the following example the function f1 is referenced under the name
>> "f1(int)", but it should be "A<int> f1<int>(int)".
>> 
>> $ cat template.cc
>> template <class X>
>> struct A
>> {
>> X x;
>> friend A f1 (X);
>> };
>> 
>> template <class X> A<X> f1 (X) { A<X> a; return a; }
>> template <class X> A<X> f2 (const A<X>& a) { return f1 (a.x); }

> Obviously the compiler sees two different functions f1(1)

But it shouldn't.  A f1(int) is only visible inside the scope of
A<int>, because it is not declared in the global namespace.  Thus, in
the scope of f2<int>(A<int> const&), the name f1 must resolve to the
template instantiation f1<int>(int), not to the non-template function
f1(int).

> The question is, whether the compiler is allowed to regard the friend
> declaration as an ordinary function.

It must.  There's a bug in the CD2, fixed in a later edition of the
DWP (now Standard!).  The relevant sections are [temp.inject] and
[basic.lookup.koenig]

-- 
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: Template mangling bug in egcs-2.90.16
       [not found] ` <Pine.LNX.3.96.971117163642.10130A-100000.cygnus.egcs@rp168.urz.tu-dresden.de>
@ 1997-11-17 13:18   ` Jason Merrill
  1997-11-17 23:08     ` Tom Kunert
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 1997-11-17 13:18 UTC (permalink / raw)
  To: Tom Kunert, egcs

>>>>> Tom Kunert <tom@rp168.urz.tu-dresden.de> writes:

> Obviously the compiler sees two different functions f1(1) : One is the
> instantiation of a template and one is an ordinary function. According
> to the DWP these functions can coexist, the overload-resolution rules
> prefer the normal function. 

Yes.

> The friend declaration in the example is regarded as function, not as
> template, so it is undefined (U). Any call to f1(int) will result in a
> linker error: f1(int) not defined.

Yes.

> The question is, whether the compiler is allowed to regard the friend
> declaration as an ordinary function. I think, this should be a function
> template and there is a bug in egcs. (See DWP Nov. 96, 14.3.5)

No.  Guiding decls have been removed from the language.  BTW, there is no
14.3.5 in the Nov. 96 WP; what section are you referring to?

Jason

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

* Re: Template mangling bug in egcs-2.90.16
       [not found]   ` <or67prihnu.fsf.cygnus.egcs@grupiara.dcc.unicamp.br>
@ 1997-11-17 18:11     ` Jason Merrill
  0 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 1997-11-17 18:11 UTC (permalink / raw)
  To: Alexandre Oliva, egcs

>>>>> Alexandre Oliva <oliva@dcc.unicamp.br> writes:

>> Obviously the compiler sees two different functions f1(1)

> But it shouldn't.  A f1(int) is only visible inside the scope of
> A<int>, because it is not declared in the global namespace.

Agreed.  g++ doesn't implement Koenig lookup.  This isn't really a bug,
it's an unimplemented feature.

Jason

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

* Re: Template mangling bug in egcs-2.90.16
  1997-11-17 13:18   ` Jason Merrill
@ 1997-11-17 23:08     ` Tom Kunert
  1997-11-17 23:16       ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Kunert @ 1997-11-17 23:08 UTC (permalink / raw)
  To: Jason Merrill; +Cc: egcs

> > The question is, whether the compiler is allowed to regard the friend
> > declaration as an ordinary function. I think, this should be a function
> > template and there is a bug in egcs. (See DWP Nov. 96, 14.3.5)
> 
> No.  Guiding decls have been removed from the language.  BTW, there is no
> 14.3.5 in the Nov. 96 WP; what section are you referring to?
> 
> Jason
> 

Oops. Sorry. I refer to the example in section 14.5.3. There is a friend
function, which depends on the template-parameter. This function is
defined as a function-template. 

  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) { /* ... */ }
   --end example]



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

* Re: Template mangling bug in egcs-2.90.16
  1997-11-17 23:08     ` Tom Kunert
@ 1997-11-17 23:16       ` Jason Merrill
  0 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 1997-11-17 23:16 UTC (permalink / raw)
  To: Tom Kunert; +Cc: egcs

>>>>> Tom Kunert <tom@rp168.urz.tu-dresden.de> writes:

> Oops. Sorry. I refer to the example in section 14.5.3. There is a friend
> function, which depends on the template-parameter. This function is
> defined as a function-template. 

That example was obsolete, and in any case examples are not normative.  The
October 1997 draft (not public comment) is very explicit:

  14.5.5  Function templates                                  [temp.fct]

2 A function template can be overloaded with  other  function  templates
  and  with  normal  (non-template) functions.  A normal function is not
  related to a function template (i.e., it is never considered to  be  a
  specialization),  even  if  it  has the same name and type as a poten-
  tially generated function template specialization.5)

  _________________________
  5) That is, declarations of non-template functions do not merely guide
  overload resolution of template functions with the same name.  If such
  a non-template function is used in a program, it must be  defined;  it
  will  not be implicitly instantiated using the function template defi-
  nition.

Jason

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

end of thread, other threads:[~1997-11-17 23:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-11-17  2:12 Template mangling bug in egcs-2.90.16 Andreas Schwab
1997-11-17  8:12 ` Tom Kunert
1997-11-17 12:30   ` Alexandre Oliva
     [not found]   ` <or67prihnu.fsf.cygnus.egcs@grupiara.dcc.unicamp.br>
1997-11-17 18:11     ` Jason Merrill
     [not found] ` <Pine.LNX.3.96.971117163642.10130A-100000.cygnus.egcs@rp168.urz.tu-dresden.de>
1997-11-17 13:18   ` Jason Merrill
1997-11-17 23:08     ` Tom Kunert
1997-11-17 23:16       ` Jason Merrill

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).