C++ Logo


Advanced search

Explicit instantiation declarations and requires clauses

From: Christopher Head <chead_at_[hidden]>
Date: Sun, 8 Sep 2019 09:54:50 -0700
Hi folks,
From reading the standard draft, I’m reasonably certain (though could
be wrong—please correct me if so) that, if a template has a requires
clause, the requires clause is checked for an explicit instantiation
declaration. GCC also seems to work this way (and is the only compiler
I have access to that supports concepts at all, so is the only one I
could test).

Why is this so? It seems to me that it’s both unnecessary and also
somewhat annoying.

It’s unnecessary, as far as I can see, because any program containing an
explicit instantiation declaration, assuming that specialization is
used anywhere, must also contain a matching explicit instantiation
definition in some translation unit; therefore, to ensure that the
requires clause is checked properly, it would be sufficient (in the
sense that the constraint would be checked somewhere and the program as
a whole would fail to compile on constraint violation) to check the
requirements at the point of explicit instantiation definition, while
ignoring them at the explicit instantiation declaration.

It’s annoying because it means an explicit instantiation declaration
can no longer use an incomplete type as a template parameter, which
used to be possible in the absence of a requires clause (the explicit
instantiation definition would typically require a complete type
because the template being instantiated would typically need the type
to be complete in order to do interesting things with it, but the
explicit instantiation declaration may not—for example, a class
template whose class body doesn’t require a complete type but whose
function bodies do, and whose function bodies are provided in a single
translation unit rather than in a header file, should be able to use an
incomplete type in the header file and a complete type in the source

Can anyone explain if (1) I have understood the situation correctly,
and (2) there is any reason why explicit instantiation declarations
need to check requires clauses?

Thank you!
Christopher Head

Received on 2019-09-08 11:57:01