Clang accepts explicit instantiation of class-template member outside its namespace
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
**What code / commands /steps will reproduce the problem?**
Compile the sample code:
```c++
namespace A {
template
struct My_Class {
template
static constexpr C S = C(1);
};
}
template
template
constexpr C A::My_Class::S;
using namespace A;
// Invalid: should be inside namespace A
template const int My_Class::S;
int main() {}
````
**Compiler Explorer link:**
https://godbolt.org/z/8rdYf9dzd
**What is the expected result?**
The program is ill-formed; the compiler should diagnose the misplaced explicit instantiation.
As per the standard:
A class, function, variable, or member template specialization can be explicitly instantiated from its template.
A member function, member class or static data member of a class template can be explicitly instantiated
from the member definition associated with its class template. An explicit instantiation of a function template
or member function of a class template shall not use the inline or constexpr specifiers.
The syntax for explicit instantiation is:
explicit-instantiation:
externopt template declaration
There are two forms of explicit instantiation: an explicit instantiation definition and an explicit instantiation
declaration. An explicit instantiation declaration begins with the extern keyword.
An explicit instantiation “shall appear in an enclosing namespace of its template;” placing it at global scope is ill-formed. Compiler should issue a diagnostic or reject.
**What happens instead?**
Compilation is successful.
Invalid code is accepted; masks namespace mistakes and can lead to ODR issues.
Contributor guide
Research direction
Start by compiling the provided C++ sample with Clang and comparing its result with the expected namespace rule for explicit instantiation. Trace Clang's handling of explicit instantiation declarations and add a regression test based on the sample; done means the misplaced instantiation produces a diagnostic.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100