'Template specialization in .cpp file + primary template declaration in .h file

According to https://eel.is/c++draft/temp.expl.spec#7:

If a template, a member template or a member of a class template is explicitly specialized, a declaration of that specialization shall be reachable from every use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.

Therefore I wonder, is the following program ill-formed, NDR?

// foo.h
template <typename T>
void foo();

// Specialization not declared in the header!

// foo.cpp
#include "foo.h"

template <>
void foo<int>()
{
 // ...
}

// main.cpp
#include "foo.h"

int main()
{
    foo<int>();
}


Solution 1:[1]

I think, according to [temp.inst]/10 the call to foo<int> in main should cause implicit instantiation of a declaration for foo<int>.

Then by the quote you have given, which is referring to implicit instantiations in general, not only implicit instantiations of definitions, I would say the program is ill-formed, no diagnostic required.

In CWG issue 2138 the analogous situation for a member function of a class template is considered and it was closed as not-a-defect with the rationale that the construct is ill-formed, no diagnostic required.

There is also an as of yet without response editorial issue regarding this (https://github.com/cplusplus/draft/issues/4816).

You could add a declaration of the explicit instantiation in foo.h to satisfy the requirement of your quote:

// foo.h
template <typename T>
void foo();

template <>
void foo<int>();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1