0

I am well aware of the fact that the template function basically gets compiled at the point when there is a specific invocation. But in this question I am looking for something different.

I have the following source files :

a.cpp :

template <class T>
void g(T x) {
    cout<<x<<"  g#1\n" ;
}
void f() ;
main() {
    g(10) ;
    g('c') ;
    f() ;
}

b.cpp :

template <class T>
void g(T x) {
    cout<<x<<"  g#2\n" ;
}
void f() {
    g(11) ;
}

Now when I compile the code as :

g++ a.cpp b.cpp

all the three calls to g (2 in main and one in function 'f') are resolved via the template function defined in a.cpp.

But when I compile the code as :

g++ b.cpp a.cpp

the call g(10) in main and g(11) in function 'f' are resolved via the template function defined in b.cpp while the call g('c') in main is resolved via the template function defined in a.cpp

Of course if the template function defined in either of the two files is removed there is a compiler error reported by that particular file.

Can someone please provide an explanation for all these observations

In particular I want to know what kind of function call resolution is taking place during object file generation a.o and b.o. My query is as to how is the call resolved at compile time, i.e when I say :

g++ -c a.cpp

and

g++ -c b.cpp

and then what happens when I say :

g++ a.o b.o
user3282758
  • 1,379
  • 11
  • 29

0 Answers0