C++ Logo

std-discussion

Advanced search

Why does the instantiation context of "foo<int, X>" comprise the end of TU2?

From: jim x <xmh970252187_at_[hidden]>
Date: Wed, 29 Sep 2021 15:51:51 +0800
Hi, everyone.

I have a concern about the formal example in [module.context] p7

//Translation unit #1:
export module stuff;export template<typename T, typename U> void
foo(T, U u) { auto v = u; }export template<typename T, typename U>
void bar(T, U u) { auto v = *u; }
//Translation unit #2:
export module M1;import "defn.h"; // provides struct X
{};import stuff;export template<typename T> void f(T t) {
  X x;
  foo(t, x);
}
// Translation unit #3:
export module M2;import "decl.h"; // provides struct X; (not a
definition)import stuff;export template<typename T> void g(T t) {
  X *x;
  bar(t, x);
}
// Translation unit #4:
import M1;import M2;void test() {
  f(0);
  g(0);
}

For f(0), the comment says that

the instantiation context of foo<int, X> comprises


   - the point at the end of translation unit #1,
   - the point at the end of translation unit #2, and
   - the point of the call to f(0),


I can understand the first point, because of this rule

During the implicit instantiation of a template whose point of
instantiation is specified as that of an enclosing specialization
([temp.point]), the instantiation context is the union of the instantiation
context of the enclosing specialization and, *if the template is defined in
a module interface unit of a module M and the point of instantiation is not
in a module interface unit of M, the point at the end of the
declaration-seq of the primary module interface unit of M (prior to the
private-module-fragment, if any)*.

However, I cannot figure out the remaining points, especially the second
one. How to interpret such two points? Which bullet says the instantiation
context of "enclosing specialization" of `f` can comprise the end of
`TU2`? According to [module.context#5], the point of instantiation of
`f<0>` should either immediately follow the definition of function `test`
or at the end of "TU4", which could be the instantiation context of
"foo<int,X>", except that there is no rule in [module.context] specifies
that the end of "TU2" is a point. Appreciate for answers.

Received on 2021-09-29 02:52:06