Question about implicit template instantiation in C++ modules.

Takafumi Kubota takafumi.kubota1012@sslab.ics.keio.ac.jp
Tue May 26 06:39:58 GMT 2020


Thank you for the clarification!

Takafumi

2020年5月25日(月) 23:35 Nathan Sidwell <nathan@acm.org>:

> [redirected to gcc-help]
>
> On 5/25/20 12:27 AM, Kubota Takafumi wrote:
> > Hi all,
> >
> > I am studying C++ modules and, especially, trying to understand how
> implicitly
> > instantiated template specializations are exported.
>
> Thanks for trying things out!
>
> > I encounter an example where GCC and Clang behave differently.
> >
> > For example:
> >
> > |/* mod1.cpp */ export module mod1; export { // function template
> definition
> > template <typename T> T tmpl (T t) { return t; } // explicit
> instantiation of
> > "char tmpl<char>" template char tmpl (char t); // implicit instantiation
> of "int
> > tmpl<int>" in exported section int f(int i) { return tmpl(i); } } //
> implicit
> > instantiation of "double tmpl<double>" in not-exported section double
> g(double
> > d) { return tmpl(d); } /* main1.cpp */ import mod1; int main() {
> tmpl('c'); //
> > #1 tmpl(1); // #2 tmpl(1.0); // #3 return 0; } |
> >
> > Here, GCC reuses the explicitly instantiated specialization from mod1 at
> #1.
> >
> > But, it implicitly re-instantiates at #2 and #3.
> >
> > On the other hand, Clang reuses all specializations from module mod1 for
> each point.
>
> Neither compiler is wrong.   It is an implementation choice.  All such
> implicit
> instantiations, regardless of origin are required to be ODR-same.  I chose
> not
> to emit implicit instantiations, unless they are referenced by something
> else in
> the emitted graph. (it is not at all obvious which strategy is better)
>
> > So, I read the sections about modules and template instantiations in the
> working
> > draft of the technical specification [0][1][2].
> >
> > However, as far as my understanding, there is no explanation about how
> > implicitly instantiated template specializations are exported.
>
> Instantiations (explicit, implicit or partial) are not exported.  Only
> named
> entities may be exported. (using the language's definition of 'export',
> not the
> closely related implementation concept of 'emitted into a Compiled Module
> Interface'.
>
> > According to the comment of gcc/cp/module.c, there seem to be two design
> choices
> > for exporting implicit specializations: re-instantiating them on-demand
> (GCC's
> > style) and streaming them (Clang's style) in modules.
> >
> > So my questions are:
> >
> > *Does how implicit specializations are exported depend on compiler
> implementations?*
>
> Correct.
>
> > if not,
> >
> > *Does anyone know the expected behavior based on the technical
> specification?*
>
> The std requires that a well formed program cannot tell the difference :)
>
> >
> > Best Regards,
> >
> > Takafumi.
> >
> > [0]
> > <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf
> >
> > [1] <http://eel.is/c++draft/>http://eel.is/c++draft/
> >
> > [2]
> > <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf
> >
> >
> > --
> > Takafumi Kubota
> > Ph.D. student at Keio University
> > http://tk1012.github.io/
>
>
> --
> Nathan Sidwell
>


-- 
Takafumi Kubota
Ph.D. student at Keio University
http://tk1012.github.io/


More information about the Gcc-help mailing list