From: laughton@cu27.crl.aecl.ca (Peter J. Laughton)
Subject: Bug(s) in instantiating functions from function templates?
Organization: GNUs Not Usenet
Distribution: gnu
Date: Mon, 4 Jan 1993 10:16:29 GMT

I am just learning C++ by working my way through Lipmann's 2nd
edition, so the bug might be in my memory, not the computer's.

I am using GNU C++ 2.3.3 on a DECstation running Ultrix 4.2A.  I have
not yet installed gas.  I used `configure --target=decstation-ultrix4.2'.

I hope this note will help rather than hinder.

==========================================================
Bug(s) in instantiating functions from function templates?
==========================================================

Here is foo.cc:

---------------------------------------------------------------------------
#include <iostream.h>

class Double {
public:
     Double(double x) : val(x), type("Double") {}
     double val;
     char *type;
};

class Int {
public:
     Int (int x) : val(x), type("Int") {}
     int val;
     char *type;
};

template <class T>
char *typeString(T &x)
{
     return x.type;
}


main()
{
     Double x(3.0);
     Int i(4);

     cout << x.type << endl;
     cout << i.type << endl;

     // g++ generates error messages
     // as0: Error: foo.cc, line 19: .ent must precede the definition of the symbol typeString
     // as0: Error: foo.cc, line 19: Conflicting definition of symbol typeString
     // as0: Error: foo.cc, line 19: .ent must precede the definition of the symbol typeString
     // as0: Error: foo.cc, line 19: Conflicting definition of symbol typeString

     // If I comment out either of the next two lines, the code compiles fine and 
     // writes "Double" or "Int", as expected.

     cout << typeString(x) << endl;
     cout << typeString(i) << endl;

     return 0;
}
---------------------------------------------------------------------------}


g++ -S foo.cc generates the following. (Label typeString appears to be defined
more than once.)

	.file	1 "foo.cc"

 # GNU C++ 2.3.3 [AL 1.1, MM 33] DECstation running ultrix compiled by GNU C

 # Cc1 defaults:

 # Cc1 arguments (-G value = 8, Cpu = default, ISA = 1):
 # -quiet -dumpbase -o

gcc2_compiled.:
	.sdata
	.align	2
$LC0:
	.ascii	"Double\000"
	.text
	.align	2

	.loc	1 5
	.ent	__6Doubled
__6Doubled:
	.frame	$fp,8,$31		# vars= 0, regs= 1/0, args = 0, extra= 0
	.mask	0x40000000,-8
	.fmask	0x00000000,0
	subu	$sp,$sp,8
	sw	$fp,0($sp)
	move	$fp,$sp
	move	$3,$4
	sw	$6,16($fp)
	sw	$7,20($fp)
	l.d	$f0,16($fp)
	s.d	$f0,0($3)
	la	$2,$LC0
	sw	$2,8($3)
$L231:
	move	$2,$3
	j	$L230
	j	$L230
$L230:
	move	$sp,$fp			# sp not trusted here
	lw	$fp,0($sp)
	addu	$sp,$sp,8
	j	$31
	.end	__6Doubled
	.sdata
	.align	2
$LC1:
	.ascii	"Int\000"
	.text
	.align	2

	.loc	1 12
	.ent	__3Inti
__3Inti:
	.frame	$fp,8,$31		# vars= 0, regs= 1/0, args = 0, extra= 0
	.mask	0x40000000,-8
	.fmask	0x00000000,0
	subu	$sp,$sp,8
	sw	$fp,0($sp)
	move	$fp,$sp
	move	$3,$4
	sw	$5,12($fp)
	lw	$2,12($fp)
	sw	$2,0($3)
	la	$2,$LC1
	sw	$2,4($3)
$L233:
	move	$2,$3
	j	$L232
	j	$L232
$L232:
	move	$sp,$fp			# sp not trusted here
	lw	$fp,0($sp)
	addu	$sp,$sp,8
	j	$31
	.end	__3Inti
	.align	2
	.globl	main

	.loc	1 25
	.ent	main
main:
	.frame	$fp,64,$31		# vars= 24, regs= 6/0, args = 16, extra= 0
	.mask	0xc00f0000,-4
	.fmask	0x00000000,0
	subu	$sp,$sp,64
	sw	$31,60($sp)
	sw	$fp,56($sp)
	sw	$19,52($sp)
	sw	$18,48($sp)
	sw	$17,44($sp)
	sw	$16,40($sp)
	move	$fp,$sp
	jal	__main
	addu	$4,$fp,16
	li.d	$6,3.00000000000000000000e+00
	jal	__6Doubled
	addu	$2,$fp,32
	move	$4,$2
	li	$5,0x00000004		# 4
	jal	__3Inti
	la	$4,cout
	lw	$5,24($fp)
	jal	__ls__7ostreamPCc
	move	$16,$2
	move	$4,$16
	la	$5,endl__FR7ostream
	jal	__ls__7ostreamPFR7ostream_R7ostream
	la	$4,cout
	lw	$5,36($fp)
	jal	__ls__7ostreamPCc
	move	$17,$2
	move	$4,$17
	la	$5,endl__FR7ostream
	jal	__ls__7ostreamPFR7ostream_R7ostream
	addu	$4,$fp,16
	jal	typeString
	la	$4,cout
	move	$5,$2
	jal	__ls__7ostreamPCc
	move	$18,$2
	move	$4,$18
	la	$5,endl__FR7ostream
	jal	__ls__7ostreamPFR7ostream_R7ostream
	addu	$2,$fp,32
	move	$4,$2
	jal	typeString
	la	$4,cout
	move	$5,$2
	jal	__ls__7ostreamPCc
	move	$19,$2
	move	$4,$19
	la	$5,endl__FR7ostream
	jal	__ls__7ostreamPFR7ostream_R7ostream
	move	$2,$0
	j	$L234
	j	$L234
	move	$2,$0
	j	$L234
	j	$L234
$L234:
	move	$sp,$fp			# sp not trusted here
	lw	$31,60($sp)
	lw	$fp,56($sp)
	lw	$19,52($sp)
	lw	$18,48($sp)
	lw	$17,44($sp)
	lw	$16,40($sp)
	addu	$sp,$sp,64
	j	$31
	.end	main
	.align	2

	.loc	1 19
	.ent	typeString
typeString:
	.frame	$fp,8,$31		# vars= 0, regs= 1/0, args = 0, extra= 0
	.mask	0x40000000,-8
	.fmask	0x00000000,0
	subu	$sp,$sp,8
	sw	$fp,0($sp)
	move	$fp,$sp
	sw	$4,8($fp)
	lw	$3,8($fp)
	lw	$2,4($3)
	j	$L235
	j	$L235
$L235:
	move	$sp,$fp			# sp not trusted here
	lw	$fp,0($sp)
	addu	$sp,$sp,8
	j	$31
	.end	typeString
	.align	2

	.loc	1 19
	.ent	typeString
typeString:
	.frame	$fp,8,$31		# vars= 0, regs= 1/0, args = 0, extra= 0
	.mask	0x40000000,-8
	.fmask	0x00000000,0
	subu	$sp,$sp,8
	sw	$fp,0($sp)
	move	$fp,$sp
	sw	$4,8($fp)
	lw	$3,8($fp)
	lw	$2,8($3)
	j	$L236
	j	$L236
$L236:
	move	$sp,$fp			# sp not trusted here
	lw	$fp,0($sp)
	addu	$sp,$sp,8
	j	$31
	.end	typeString

-- 

Peter J. Laughton 

Reactor Physics Branch             Internet: laughton@cu27.crl.aecl.ca
AECL Research                         Phone: (613) 584-3311, x. 4267
Chalk River Laborartories               FAX: (613) 584-1108
Chalk River, Ontario
CANADA  K0J 1J0

Mail station:  68

Newsgroups: gnu.g++.bug