Issue 176922
Summary Clang accepts explicit instantiation of class-template member outside its namespace
Labels clang
Assignees
Reporter vrukesh
    **What code / commands /steps will reproduce the problem?**
Compile the sample code:
`
namespace A {
  template<class B>
  struct My_Class {
    template<class C>
    static constexpr C S = C(1);
  };
}

template<class B>
template<class C>

constexpr C A::My_Class<B>::S;

using namespace A;

// Invalid: should be inside namespace A
template const int My_Class<int>::S<int>;
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.


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to