Topic: Please define "template definition context
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sat, 19 Jun 2010 17:18:29 CST Raw View
What does constitute the template definition context? Consider the following
example
struct B { };
template<typename T>
struct A : ATemplate<T> {
void f() { g(T()); }
};
"g" is a dependent name. When A<T>::f is instantiated, i see three things of
interest. Let's assume instantiation occurs as follows:
int main() {
A<B>().f();
}
- Context in definition when parsing the definition.
- Context in definition when instantiating the definition.
- Instantiation context
The third one is defined in 14.6.4.1/6. And the term "template definition
context" is used in 14.6.4.2/1 for defining where unqualified lookup occurs
for "g". What of the first two items above apply? I ask, because in C++98
before the C++03 revision, unqualified dependent names were allowed to find
declarations of dependent base classes, which was changed by DR213. In that
DR, Erwin Unruh seems to imply that the first item above applies, but the
reporter, John Spicer, seems to imply the second definition of above apply.
In other words, in the second item, we can find declarations in the base
class because with that definition, "ATemplate<T>" is known to be
"ATemplate<B>" and declarations in that class could be analyzed and matched
like with the interpretation John Spicer seems to use.
Any ideas?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Mon, 21 Jun 2010 00:24:06 CST Raw View
On 20 Jun., 01:18, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> What does constitute the template definition context? Consider the following
> example
>
> struct B { };
>
> template<typename T>
> struct A : ATemplate<T> {
> void f() { g(T()); }
> };
>
> "g" is a dependent name. When A<T>::f is instantiated, i see three things of
> interest. Let's assume instantiation occurs as follows:
>
> int main() {
> A<B>().f();
> }
>
> - Context in definition when parsing the definition.
This is my understanding of the template definition context.
It seems quite natural, because this can be easily interpreted
as the "context at the point of definition of the template",
see the wording used in 14.6.4 [temp.dep.res]. The "point
of definition" seems to be the "point of declaration" (3.3.2
[basic.scope.pdecl]/3), when the declaration is also a definition.
> - Context in definition when instantiating the definition.
I'm not sure what this would be. This seems basically to
be equal to the instantiation context, except for some minor
constraints described in 14.6.4.2 [temp.dep.candidate],
if I understand you correctly. To be sure, let us complete
your example a bit, starting with
struct B { };
template<class>
struct ATemplate; // Incomplete
template<typename T>
struct A : ATemplate<T> {
void f() { g(T()); }
};
template<>
struct ATemplate<B> {
// ...
};
int main() {
A<B>().f();
}
Given this example, I wouldn't understand how
your second interpretation could work, because
either the definition of the instantiated specialization
A<B> would be ill-formed (because ATemplate<B>
is still an incomplete type) or this definition would
require that an implementation has to "push up"
(at least conceptually) the definition of ATemplate<B>
before that of A<B> when this still is located at the
point of parsing. The first choice looks very
undesirable and the second choice causes more
undesirable implications on the meaning of names
within the definition of ATemplate<B> at this point.
> - Instantiation context
>
> The third one is defined in 14.6.4.1/6. And the term "template definition
> context" is used in 14.6.4.2/1 for defining where unqualified lookup occurs
> for "g". What of the first two items above apply? I ask, because in C++98
> before the C++03 revision, unqualified dependent names were allowed to find
> declarations of dependent base classes, which was changed by DR213. In that
> DR, Erwin Unruh seems to imply that the first item above applies, but the
> reporter, John Spicer, seems to imply the second definition of above apply.
>
> In other words, in the second item, we can find declarations in the base
> class because with that definition, "ATemplate<T>" is known to be
> "ATemplate<B>" and declarations in that class could be analyzed and matched
> like with the interpretation John Spicer seems to use.
I'm not sure about the exact reason of the different views, but I
doubt
that it is due to different interpretations on the meaning of
"template
definition context". It might have been related to the problem how
ADL was understood - and there is still an open issue related to that,
see:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#405
The last assumption is really just a guess.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Mon, 21 Jun 2010 17:36:22 CST Raw View
Daniel Kr=FCgler wrote:
> On 20 Jun., 01:18, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> wrote:
>> What does constitute the template definition context? Consider the
>> following example
>>
>> struct B { };
>>
>> template<typename T>
>> struct A : ATemplate<T> {
>> void f() { g(T()); }
>> };
>>
>> "g" is a dependent name. When A<T>::f is instantiated, i see three thing=
s
>> of interest. Let's assume instantiation occurs as follows:
>>
>> int main() {
>> A<B>().f();
>> }
>>
>> - Context in definition when parsing the definition.
>
> This is my understanding of the template definition context.
> It seems quite natural, because this can be easily interpreted
> as the "context at the point of definition of the template",
> see the wording used in 14.6.4 [temp.dep.res]. The "point
> of definition" seems to be the "point of declaration" (3.3.2
> [basic.scope.pdecl]/3), when the declaration is also a definition.
>
This would be my suspicion too.
>> - Context in definition when instantiating the definition.
>
> I'm not sure what this would be. This seems basically to
> be equal to the instantiation context, except for some minor
> constraints described in 14.6.4.2 [temp.dep.candidate],
> if I understand you correctly. To be sure, let us complete
> your example a bit, starting with
>
> struct B { };
>
> template<class>
> struct ATemplate; // Incomplete
>
> template<typename T>
> struct A : ATemplate<T> {
> void f() { g(T()); }
> };
>
> template<>
> struct ATemplate<B> {
> // ...
> };
>
> int main() {
> A<B>().f();
> }
>
> Given this example, I wouldn't understand how
> your second interpretation could work, because
> either the definition of the instantiated specialization
> A<B> would be ill-formed (because ATemplate<B>
> is still an incomplete type) or this definition would
> require that an implementation has to "push up"
> (at least conceptually) the definition of ATemplate<B>
> before that of A<B> when this still is located at the
> point of parsing. The first choice looks very
> undesirable and the second choice causes more
> undesirable implications on the meaning of names
> within the definition of ATemplate<B> at this point.
>
What i understand under "definition context" is:
- The generated specialization when the definition context is analyzed at
instantiation time, and
- The template being parsed when the definition context is analyzed at
definition time.
My rationale is that the context is the same, but it's just a different
point in time we are looking at it.
This seems radically different from the instantiation context to me. The
instantiation context only contains declarations declared in the
instantiating translation unit. But the definition context contains the
declarations visible by the template-definition (and by the generated
specialization when actual template arguments are known).
I'm mostly confused by 14.6.4/1 and 14.6.4.1 which uses the following terms
- Definition context
- Point of definition
- Instantiation context
- Point of instantiation
Let me enumerate what i think the difference is between them. My opinion no=
w
is slightly different than my above opinion about "definition context" afte=
r
reading 14.6.4 again:
- Point of definition: The parsed template with its dependent types
substituted by the template arguments.
- Definition context: The parsed template as is without knowledge of actual
template arguments. (this would then match your any my suspicion of its
meaning).
- Instantiation context: The set of declarations in the TU where the
instantiation occurs, prior to the point of instantiation.
- Point of instantiation: A location in the TU linking the instantiation
with the TU causing the instantiation.
The reason i believe "Point of definition" has the template arguments
substituted is because 14.6.4/1 in its first bullet seems to say this is
where the following name "foo" is looked up in:
template<typename T> void f() { T::foo; }
I thought that "Point of definition" and "Definition context" basically are
two terms for the same thing before, and so i thought "Definition context"
also includes knowledge about the actual template argument values.
I'm very unsure about this stuff though. What do you think about this?
>> - Instantiation context
>>
>> The third one is defined in 14.6.4.1/6. And the term "template definitio=
n
>> context" is used in 14.6.4.2/1 for defining where unqualified lookup
>> occurs for "g". What of the first two items above apply? I ask, because
>> in C++98 before the C++03 revision, unqualified dependent names were
>> allowed to find declarations of dependent base classes, which was change=
d
>> by DR213. In that DR, Erwin Unruh seems to imply that the first item
>> above applies, but the reporter, John Spicer, seems to imply the second
>> definition of above apply.
>>
>> In other words, in the second item, we can find declarations in the base
>> class because with that definition, "ATemplate<T>" is known to be
>> "ATemplate<B>" and declarations in that class could be analyzed and
>> matched like with the interpretation John Spicer seems to use.
>
> I'm not sure about the exact reason of the different views, but I
> doubt
> that it is due to different interpretations on the meaning of
> "template
> definition context". It might have been related to the problem how
> ADL was understood - and there is still an open issue related to that,
> see:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#405
>
> The last assumption is really just a guess.
>
Thanks for all the help on the matter!
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]