Topic: Template visibility / linkage
Author: usenet@disemia.com (edA-qa mort-ora-y)
Date: Tue, 22 Jun 2004 17:53:30 +0000 (UTC) Raw View
I am having difficulty determining what the proper linkage/locating of
templates and their specializations (Section 3.5.4 briefly mentions
template linkage, and 14 talks about the export keyword).
Situation A:
In <header1>:
template<typename T> void func( T val );
In <src1>:
#include "header1"
template<> void func<int>( int val ) { ... }
In <src2>:
#include "header1"
..
int a;
func( a );
Comments: In this situation, should the code in <src2> end up calling
the template specialization as defined in <src1>, even though it is
unaware of its existence at compile time? Since the linkage of the
template is external, and it has the same signature, I would assume yes.
Situation B:
In <header1>:
template<typename T> void func( T val )
{ /* primary def */ }
In <src1>:
#include "header1"
template<> void func<int>( int val ) ...
In <src2>:
#include "header1"
..
int a;
func( a );
Comments: Should the code in <src2> use the primary template definition
or should it use the specialization from <src1>? My reading of the
standard indicates it should use the specializaed form, since the
template matching doesn't appear to be limited to the current
translation unit, and the the function signature will match that in
<src1> at link time.
I am unclear of whether the template matching rules hold over to link
time, or only for the current translation unit. That is, if <src2>, in
both cases, were to also include <src1> it would be clear that it usese
the specialization, but since it doesn't include <src1>, the compilation
finishes without knowledge of the specialization. Does the linker then
/do the right thing/?
And I'm completely unclear on classes, whether they should match
identical declarations in other units, as there would be signifcant
sizing problems for the compiler. Though I can't see why it would be
disallowed according to the standard.
--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>
"What suffering would man know if not for his own?"
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Wed, 23 Jun 2004 01:21:52 +0000 (UTC) Raw View
edA-qa mort-ora-y wrote:
> I am having difficulty determining what the proper linkage/locating of
> templates and their specializations (Section 3.5.4 briefly mentions
> template linkage, and 14 talks about the export keyword).
>
> Situation A:
>
> In <header1>:
> template<typename T> void func( T val );
>
> In <src1>:
> #include "header1"
> template<> void func<int>( int val ) { ... }
>
> In <src2>:
> #include "header1"
> ..
> int a;
> func( a );
>
> Comments: In this situation, should the code in <src2> end up calling
> the template specialization as defined in <src1>, even though it is
> unaware of its existence at compile time? Since the linkage of the
> template is external, and it has the same signature, I would assume yes.
This code is ill-formed. Compiling <src2> triggers an implicit
instantiation of the function template func, so either the definition of
the primary template shall be visible or the template shall be declared
with the export keyword. A non-exported declaration (as in the example)
is not enough. The presence of the specialization in <src1> is
inessential to this example. The fact that the linkage of a template is
external is completely unrelated with this example.
>
> Situation B:
>
> In <header1>:
> template<typename T> void func( T val )
> { /* primary def */ }
>
> In <src1>:
> #include "header1"
> template<> void func<int>( int val ) ...
>
> In <src2>:
> #include "header1"
> ..
> int a;
> func( a );
>
> Comments: Should the code in <src2> use the primary template definition
> or should it use the specialization from <src1>? My reading of the
> standard indicates it should use the specializaed form, since the
> template matching doesn't appear to be limited to the current
> translation unit, and the the function signature will match that in
> <src1> at link time.
This code is ill-formed too. In 14.7.3/6 it is clearly stated that:
"If a template [...] is explicitly specialized then that specialization
shall be declared before the first use of that specialization that would
cause an implicit instantiation to take place, in every translation unit
in which such a use occurs; no diagnostic is required."
>
> I am unclear of whether the template matching rules hold over to link
> time, or only for the current translation unit. That is, if <src2>, in
> both cases, were to also include <src1> it would be clear that it usese
> the specialization, but since it doesn't include <src1>, the compilation
> finishes without knowledge of the specialization. Does the linker then
> /do the right thing/?
>
The above statement leaves no doubt to me. If <src2> does not include
<src1> the program is ill-formed.
> And I'm completely unclear on classes, whether they should match
> identical declarations in other units, as there would be signifcant
> sizing problems for the compiler. Though I can't see why it would be
> disallowed according to the standard.
>
The same argument applies to class templates.
Alberto
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: usenet@disemia.com (edA-qa mort-ora-y)
Date: Wed, 23 Jun 2004 19:48:32 +0000 (UTC) Raw View
Alberto Barbati wrote:
> with the export keyword. A non-exported declaration (as in the example)
> is not enough. The presence of the specialization in <src1> is
> inessential to this example. The fact that the linkage of a template is
> external is completely unrelated with this example.
Thank you. I think I understand now. One question with the export
keyword though, must this go on only the declaration? Does it then
apply to all specializations in other translation units -- or must the
specializations still be seen (in declaration only with export) in the
translation unit that instantiates them?
--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>
"What suffering would man know if not for his own?"
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Fri, 25 Jun 2004 19:38:42 +0000 (UTC) Raw View
edA-qa mort-ora-y wrote:
> Alberto Barbati wrote:
>
>> with the export keyword. A non-exported declaration (as in the
>> example) is not enough. The presence of the specialization in <src1>
>> is inessential to this example. The fact that the linkage of a
>> template is external is completely unrelated with this example.
>
>
> Thank you. I think I understand now. One question with the export
> keyword though, must this go on only the declaration? Does it then
> apply to all specializations in other translation units -- or must the
> specializations still be seen (in declaration only with export) in the
> translation unit that instantiates them?
>
>
I don't completely understand how export works, so I leave to others the
answer to your question. AFAIK only EDG-based compilers support the
export keyword and I did't have yet the opportunity to use any such
compiler. Your curiosity is praiseworthy, but unless you really have a
compiler supporting export, I wouldn't mess with it.
Alberto
PS: in case you are wondering, no, neither gcc nor VC++ support the
export keyword
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]