M.M
M.M

Reputation: 141618

Calling function templates specialized in another translation unit

I'm working on a codebase which uses the following structure:

a.h:

template<int N> void f();
void b();

a.cpp:

#include "a.h"

template<> void f<1>() {}

int main()
{
    b();
}

b.cpp:

#include "a.h"
void b()
{
    f<1>();
}

The code appears to build and run correctly.

My question is: is this well-formed, or is it some kind of ill-formed NDR that happens to work?


If building with clang -Wundefined-func-template (this was enabled in my IDE's default settings for clang-tidy) then a warning is produced:

b.cpp:5:2: warning: instantiation of function 'f<1>' required here, but no definition is available [-Wundefined-func-template]
        f<1>();
        ^
./a.h:1:22: note: forward declaration of template entity is here
template<int N> void f();
                     ^
b.cpp:5:2: note: add an explicit instantiation declaration to suppress this warning if 'f<1>' is explicitly instantiated in another translation unit
        f<1>();
        ^

But I am not sure whether to just disable the warning, or make some code change (other than moving the explicit specialization definition to the header file, which would not be preferable for this project).

Following the advice in the warning message and adding an explicit instantiation declaration to the header file (i.e. extern template void f<1>();) caused an error message (implicit instantiation of a specialization before explicit instantiation).

However, adding an explicit specialization declaration template<> void f<1>(); to the header file suppresses the warning. But I am not sure if this is (a) necessary, and/or (b) recommended style.

Upvotes: 2

Views: 1602

Answers (3)

Language Lawyer
Language Lawyer

Reputation: 3569

[temp]/7 says:

A function template, member function of a class template, variable template, or static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated unless the corresponding specialization is explicitly instantiated in some translation unit; no diagnostic is required.

The standard requires explicit instantiation, so explicit specialization in a.cpp won't make the program well-formed.

A similar question ([temp]/7 treats function templates and member functions of class templates equally) was asked in CWG2138:

It is not clear whether the following common practice is valid by the current rules:

   // foo.h
   template<typename T> struct X {
    int f(); // never defined
   };
   // foo.cc
   #include "foo.h"
   template<> int X<int>::f() { return 123; }
   // main.cc
   #include "foo.h"
   int main() { return X<int>().f(); }

which was closed as NAD with the following rationale:

As stated in the analysis [which referred to [temp]/7, among other things], the intent is for the example to be ill-formed, no diagnostic required.

So, the answer is: the program is ill-formed NDR, and this is intended.

Upvotes: 4

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

There's an example in [temp.over]/5 that matches yours almost exactly, and pronounces it well-formed:

[temp.over]/5 ... [ Example:

template<class T> void f(T); // declaration
void g() {
  f("Annemarie"); // call of f<const char*>
}

The call of f is well-formed even if the template f is only declared and not defined at the point of the call. The program will be ill-formed unless a specialization for f<const char*>, either implicitly or explicitly generated, is present in some translation unit. —end example ]

Upvotes: 2

aschepler
aschepler

Reputation: 72401

The program violates [temp.expl.spec]/6:

If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first 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.

The function template f is explicitly specialized by template<> void f<1>() {} in b.cpp. But in the translation unit formed from b.cpp and including a.h, the statement f<1>(); would cause an implicit instantiation of the same specialization f<1>, and there is no declaration of the explicit specialization earlier (or anywhere) in the translation unit.

Per the Standard, an explicit specialization is always a distinct thing from an instantiated specialization, since both can never exist for the same primary template and same template arguments. But the program might work anyway because many compilers use the same mangled linker names for template explicit specializations and instantiated specializations.

The clang warning might be because it's legal, though unusual, to implicitly instantiate a function template without a visible definition if the same specialization is explicitly instantiated, not explicitly specialized, elsewhere. So it's suggesting an improvement to make a legal program clearer. I'm not exactly sure if it actually is legal, though. But its suggested explicit instantiation declaration would be a lie, since the specialization is explicitly specialized, not explicitly instantiated.

The program does become valid if you add explicit specialization declarations to the header file for every specialization which will be used.

template<int N> void f();
template<> void f<1>();

Upvotes: 1

Related Questions