473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

context in template point of instantiation (g++)

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 TT 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

Jan 16 '07 #1
4 1797

ca******************@gmail.com napsal:
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 TT 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
In
void g2()
{
cout << Applyf(1.0) << endl ;
}

Is called Applyf<doubleand it calls f(double). You have to change
Applyf(1.0) to Applyf(1.0f) to call Applyf<float>.

Anyway I do not know, why f(double) is found in g2.C compilation unit.

Jan 16 '07 #2

Ondra Holub wrote:
ca******************@gmail.com napsal:
Hello,
[............]

Is called Applyf<doubleand it calls f(double). You have to change
Applyf(1.0) to Applyf(1.0f) to call Applyf<float>.

Anyway I do not know, why f(double) is found in g2.C compilation unit.
I know you can easily direct the compiler to any version of 'f' you
wish, but.... the problem is that the criteria it uses (when you do not
that and it has to choose) seems to be rather unexpected.

Jan 16 '07 #3
ca******************@gmail.com wrote:
Ondra Holub wrote:
ca******************@gmail.com napsal:
Hello,
[............]
Is called Applyf<doubleand it calls f(double). You have to change
Applyf(1.0) to Applyf(1.0f) to call Applyf<float>.

Anyway I do not know, why f(double) is found in g2.C compilation unit.

I know you can easily direct the compiler to any version of 'f' you
wish, but.... the problem is that the criteria it uses (when you do not
that and it has to choose) seems to be rather unexpected.
Not really. The program can have only one, consistent Applyf<double>()
specialization - yet this program has created two Applyf<double>()
specialiations that differ from each other: one calls f(double) and the
other one calls f(float). Technically these two specializations for
Applyf<doubleviolate C++'s "One Definition Rule" (ODR). So the
behavior of the program is unexpected because it is undefined. As a
practical matter, which version of Applyf<doublewinds up in the
eventual build is dependent on factors that no build should depend on.

The fix should be straightfoward. A template function should call only
those functions that are "visible", that is, that have been declared
(they need not be defined) before the function template itself.
Greg

Jan 16 '07 #4

Greg wrote:
ca******************@gmail.com wrote:
Ondra Holub wrote:
ca******************@gmail.com napsal:
Hello,
[............]
>
Is called Applyf<doubleand it calls f(double). You have to change
Applyf(1.0) to Applyf(1.0f) to call Applyf<float>.
>
Anyway I do not know, why f(double) is found in g2.C compilation unit.
I know you can easily direct the compiler to any version of 'f' you
wish, but.... the problem is that the criteria it uses (when you do not
that and it has to choose) seems to be rather unexpected.

Not really. The program can have only one, consistent Applyf<double>()
specialization - yet this program has created two Applyf<double>()
specialiations that differ from each other: one calls f(double) and the
other one calls f(float). Technically these two specializations for
Applyf<doubleviolate C++'s "One Definition Rule" (ODR). So the
behavior of the program is unexpected because it is undefined. As a
practical matter, which version of Applyf<doublewinds up in the
eventual build is dependent on factors that no build should depend on.

The fix should be straightfoward. A template function should call only
those functions that are "visible", that is, that have been declared
(they need not be defined) before the function template itself.
Greg
Thanks a lot, Greg, now the issue is clear to me. I've been reading
about <a
href="http://www.slac.stanford.edu/BFROOT/www/Computing/Environment/Standards/C++/cd2/basic.html#basic.def.odr">ODR</a>
and, if I'm not wrong, it states that violations to it need not be
allways diagnosed by the compiler/linker, which is the case here with
gcc/ld (although it usually diagnoses duplicate function definitions).
I was trying to find an example in which C++ templates flexibility (in
the sense that C++ allows calls from templates to still non declared
functions) allows to introduce unexpected behavoir. It seems that I
have found one --- IMHO, Ada approach to genericity is far more safe.

Jan 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
6
by: Dmitry Epstein | last post by:
Here is an example of a problem, which I tried to reduce to its bare essentials: // begin test1.cpp class E { public: template<class T> static void f(); }; template<class T> void E::f() {}
7
by: Hunter Hou | last post by:
Hello, I'm trying one example of <<the C++ programming language>> Page 865 int f( int ); template< class T > T g( T t ) { return f( t ); } char c = g( 'a' ); ************ char f( char ); ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
5
by: lobequadrat | last post by:
Hello, I am trying to get the following code work (unfortunately not mine ... :( ) template <class Tclass Test { public: class ELEM;
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
2
by: Juha Nieminen | last post by:
Paavo Helde wrote: So which compiler is correct, gcc (which only requires bar() to be declared at the point of insantiation of foo()) or comeau? What happens if you just move the bar()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.