This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
templates and function attributes don't play well together
- To: bug-gcc at gnu dot org
- Subject: templates and function attributes don't play well together
- From: Corey Kosak <kosak at cs dot cmu dot edu>
- Date: Thu, 08 Jun 2000 18:03:11 -0400
$ cat b5.cc && g++ -v
#include <iostream.h>
template<class FP> void apply(FP func)
{
func(10);
}
void p1(int);
void p2(int) __attribute__((ATTRIBUTE));
void p1(int x)
{
cout <<"p1's argument is " <<x <<endl;
}
void p2(int x)
{
cout <<"p2's argument is " <<x <<endl;
}
int main()
{
#if P1_FIRST
apply(p1);
apply(p2);
#else
apply(p2);
apply(p1);
#endif
return 0;
}
$ g++ '-DATTRIBUTE=regparm(3)' -DP1_FIRST b5.cc && a.out
p1's argument is 10
p2's argument is 134521312
$ g++ '-DATTRIBUTE=regparm(3)' b5.cc && a.out
p2's argument is 10
p1's argument is 134521201
Notice that the compiler can generate the proper version of apply for either
p1 or p2 but not both.
------------------------------------------------------------------------
A related problem with a different function attribute:
$ g++ -DATTRIBUTE=stdcall b5.cc && a.out
/usr0/kosak/tmp/ccM9vWmC.s: Assembler messages:
/usr0/kosak/tmp/ccM9vWmC.s:138: Fatal error: Symbol apply__H1ZPFi_v_X01_v already defined.
------------------------------------------------------------------------
I'll leave the analysis to the experts, but in my opinion the fundamental
source of the problem is that function attributes really want to be part of
the language's type system, so that they can participate in template matching,
name mangling, and the like.
The examples using the first function attribute show that the compiler is
capable of emitting the right kind of 'apply' in either case (using the proper
calling convention for its indirect function call) but it evidently is emitting
only one version of 'apply' when it really needs two.
The second example shows that the compiler does correctly emit two different
versions of the 'apply' function but unfortunately assigns them the same
mangled name so that the subsequent assembly operation fails with a duplicate
symbol.