public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about implicit template instantiation in C++ modules.
@ 2020-05-25  4:27 Kubota Takafumi
  0 siblings, 0 replies; only message in thread
From: Kubota Takafumi @ 2020-05-25  4:27 UTC (permalink / raw)
  To: gcc; +Cc: nathan

Hi all,

I am studying C++ modules and, especially, trying to understand how
implicitly instantiated template specializations are exported.

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.

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.

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?*

if not,

*Does anyone know the expected behavior based on the technical
specification?*

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/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-05-25  4:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-25  4:27 Question about implicit template instantiation in C++ modules Kubota Takafumi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).