help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: template instantiation g++ 4.0x


From: Paul Pluzhnikov
Subject: Re: template instantiation g++ 4.0x
Date: Tue, 21 Mar 2006 13:43:13 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Tom H <tomh@nowhere.net> writes:

> Thanks for your quick reply. My understanding of how g++ handles
> template instantiation in 3.4 agrees with the code example. From the
> manual (for both 3.4 and 4.0),the default compiler action is:
>
> "...the compiler emits template instances in each translation unit
> that uses them, and the linker collapses them together."

This is what is supposed to (and does) happen when your
explicit instantiation request *follows* the definition of
ATemplate<T>::GetValue() (as it does in my corrected example).

What is supposed to happen when the request *preceeds* the definition
is undefined.

> So, ATemplate.cpp will have int and float versions of member
> GetValue. 

Not necessarily, and does not (with g++ 4.x).

> The header file ATemplateInst.h (which mimics the real
> system even if it isn't needed in this example) forces the
> instantiations.

No it does not (with g++ 4.x).

> Looking at the symbols in the .o files shows that 4.0 is not
> generating instances of GetValue:

And I explained to you *why* it doesn't.

> Clearly the behavior of 4.0 in regards to template instantiation has
> changed from version 3.4.x. 

Yes.

> So which version is "correct"?

Both are. Results of what you are doing are undefined, so either
outcome is "correct".

> Is it
> possible to force 4.0 to revert its actions to that of 3.4?

There may be (a combination of) flags that force g++ 4.0 to do what
g++ 3.x does. If you do find such a combination, you are only
postponing the problem (the flags may not work with g++ 4.1 or 4.2).

Why don't you simply fix the problem. If you insist on having the
requests in a separate ATemplateInst.h, simply '#include' it *after*
you have provided the template function bodies:

 $ cat ATemplate.cpp
 #include "ATemplate.h" 
 template <class T> T ATemplate<T>::GetValue () const { return n; } 
 #include "ATemplateInst.h" 

You'll need to put proper "multiple include guards" into ATemplate.h
for the above to work.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]