Topic: Template point of instantiation
Author: freyburg@gamut.stanford.edu (Brian Michael Freyburger)
Date: 1996/04/29 Raw View
Paragraph 9 of temp.point (Point of instantiation) reads:
"The point of instantiation for a template used inside another template
and not instantiated previous to an instantiation of the enclosing
template is immediately before the point of instantiation of the
enclosing template."
Does this mean the following code is ill-formed?
--- Translation Unit 1 ---
template <class B> void bar(B);
template <class A>
class Foo
{
// ...
}
template <class B> void bar(B)
{
Foo<B> f;
// ...
}
--- End Translation Unit 1 ---
--- Translation Unit 2 ---
template <class B> void bar(B);
int main()
{
bar(1);
}
--- End Translation Unit 2 ---
The way I read the standard, bar<int> is instantiated just before
main(). Since bar<int> uses Foo<int>, then Foo<int> is instantiated
just before bar. However, in this translation unit, bar has not even
been declared! I understand that compilers will need a mechanism for
generating the definition of instantiated functions -- but this
implies that they will need to procide the definitions of instantiated
classes as well. Is this the intent, or does the second translational
unit need the definition of the class Foo.
However, paragraph 5 of temp.res (Name resulution) says that:
"Three kinds of names can be used within a template definition:
--The name of the template itself, the names of the template-
parameters (_temp.param_), and names declared within the template
itself.
--Names from the scope of the template definition.
--Names dependent on a template-argument (_temp.arg_) from the scope
of a template instantiation."
To me, this implies that the above program is ill-formed, because
Foo<int> is not defined in the scope fo the template instantiation.
If I have read this all correctly, and people use a header file/source
file paradigm where everything is declared in a header (.h) file and
declared in a (.c) file, the header file corresponding to a template
declaration should include all the header files that declare names
used in the DEFINITION of the template. For non-templated functions
you would only [need to] include these header files in the source
file.
Thoughts, comments?
Brian Freyburger
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]