https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71227
--- Comment #2 from Jason Merrill <jason at gcc dot gnu.org> --- 7.3.1.2: If a friend declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the innermost enclosing namespace. The friend declaration does not by itself make the name visible to unqualified lookup (3.4.1) or qualified lookup (3.4.3). 14.7.3: A declaration of a function template, class template, or variable template being explicitly specialized shall precede the declaration of the explicit specialization. I interpret this as meaning that there needs to be a visible declaration of f before it can be specialized. But we can certainly be more helpful about it.