Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Explicit template instantiation can't be combined with specialization #38188

Closed Quuxplusone closed 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR39215
Status RESOLVED INVALID
Importance P enhancement
Reported by Ole Kniemeyer (o_kniemeyer@maxon.de)
Reported on 2018-10-08 02:22:08 -0700
Last modified on 2018-10-10 00:55:47 -0700
Version 7.0
Hardware PC MacOS X
CC dgregor@apple.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
As far as I understand the C++17 standard, the code below should compile. I.e.,
it should be possible to give the definition for an explicit template
instantiation declaration by an explicit specialization.

C++17 17.7.2.5 is
"For a given set of template arguments, if an explicit instantiation of a
template appears after a declaration of an explicit specialization for that
template, the explicit instantiation has no effect. Otherwise, for an explicit
instantiation definition the definition of a function template, a variable
template, a member function template, or a member function or static data
member of a class template shall be present in every translation unit in which
it is explicitly instantiated."

GCC and Intel compile the code without errors.

template <typename T> struct S
{
    static const char* s;
};

extern template const char* S<void>::s;

template <> const char* S<void>::s = "void";

// the following should have no effect according to standard
// template const char* S<void>::s;
Quuxplusone commented 6 years ago
[temp.spec] (12.8)/5 says:

"For a given template and a given set of template-arguments,
 — an explicit instantiation definition shall appear at most once in a program,
 — an explicit specialization shall be defined at most once in a program, as specified in 6.2, and
 — both an explicit instantiation and a declaration of an explicit specialization shall not appear in a program unless the explicit instantiation follows a declaration of the explicit specialization.
An implementation is not required to diagnose a violation of this rule."

In your case, the explicit instantiation did not follow a declaration of the
explicit specialization, so the program is ill-formed.

(Note that the standard permits an implementation to not diagnose this ill-
formed code, so the fact that GCC and ICC accept this doesn't make them non-
conforming.)
Quuxplusone commented 6 years ago

Thanks for the explanation, and sorry for reporting a non-bug.