C
carlos.urena.almagro
Hello,
I'm sorry if this post raises a question with obvious answer, or if it
has been already posted and solved. I was wondering about how the
compiler (in my case, gcc 4.0.2 on linux) handles templates
instantation, and what the standard says about that. I created a
"difficult" case, in which the meaning of a call to a function (which
is dependant) depends on the context where a template is instantiated.
I have three compilation units and a header with a template, and, to my
surprise, the output depends on....the order in which compilation units
are compiled and linked ! --- I have two overloaded versions of 'f' in
two compilation units, but the compiler issues calls to either one
depending on the order of compilation or linking. It is correct w.r.t
the standard ? it is a bug in gcc ? -- I think this is related with the
mecanism for creating single template instantiation, which can be donde
at link time, but I'm not sure about the details.
'make' creates two binaries 'a1' and 'a2', with different output, but
'a1' and 'a2' are equal except for the order compilation units are
given to 'g++'. I've included the files in the text (including
makefile)
Thanks a lot in advance for any answers,
Carlos.
file t.H
---------
template<typename T> T Applyf( T a )
{ return f(a) ; }
file t1.C
-----------
#include <iostream>
#include "t.H"
using namespace std ;
double f( double x )
{
return x*x ;
}
void g1()
{
cout << Applyf(1.0) << endl ;
}
file t2.C
-----------
#include <iostream>
#include "t.H"
using namespace std ;
double f( float x )
{
return x+x ;
}
void g2()
{
cout << Applyf(1.0) << endl ;
}
file tmain.C
-----------------
void g1() ;
void g2() ;
int main( int argc, char * argv[] )
{
g1() ;
g2();
}
file makefile:
------------------
x: a1 a2
./a1
./a2
a1:
g++ -o a1 t1.C t2.C tmain.C
a2:
g++ -o a2 t2.C t1.C tmain.C
I'm sorry if this post raises a question with obvious answer, or if it
has been already posted and solved. I was wondering about how the
compiler (in my case, gcc 4.0.2 on linux) handles templates
instantation, and what the standard says about that. I created a
"difficult" case, in which the meaning of a call to a function (which
is dependant) depends on the context where a template is instantiated.
I have three compilation units and a header with a template, and, to my
surprise, the output depends on....the order in which compilation units
are compiled and linked ! --- I have two overloaded versions of 'f' in
two compilation units, but the compiler issues calls to either one
depending on the order of compilation or linking. It is correct w.r.t
the standard ? it is a bug in gcc ? -- I think this is related with the
mecanism for creating single template instantiation, which can be donde
at link time, but I'm not sure about the details.
'make' creates two binaries 'a1' and 'a2', with different output, but
'a1' and 'a2' are equal except for the order compilation units are
given to 'g++'. I've included the files in the text (including
makefile)
Thanks a lot in advance for any answers,
Carlos.
file t.H
---------
template<typename T> T Applyf( T a )
{ return f(a) ; }
file t1.C
-----------
#include <iostream>
#include "t.H"
using namespace std ;
double f( double x )
{
return x*x ;
}
void g1()
{
cout << Applyf(1.0) << endl ;
}
file t2.C
-----------
#include <iostream>
#include "t.H"
using namespace std ;
double f( float x )
{
return x+x ;
}
void g2()
{
cout << Applyf(1.0) << endl ;
}
file tmain.C
-----------------
void g1() ;
void g2() ;
int main( int argc, char * argv[] )
{
g1() ;
g2();
}
file makefile:
------------------
x: a1 a2
./a1
./a2
a1:
g++ -o a1 t1.C t2.C tmain.C
a2:
g++ -o a2 t2.C t1.C tmain.C