Issue 183597
Summary Clang accepts invalid forward-declared scoped enum with dependent underlying type in class template
Labels clang
Assignees
Reporter vrukesh
    **What code / commands /steps will reproduce the problem?**
Compile the below sample code:
`
template<class T>
struct color {
    enum city : int;
};

template<class T>
enum color<T>::city : T {
    london, ny, paris
};

int main() {
    (void)color<int>::london;
}
`

Compiler Explorer link: https://godbolt.org/z/frcn8jqK6

**What is the expected result?**
This is ill-formed code and must be rejected by the compiler with compile-time error.


C++17 ISO/IEC 14882:2017 Section 10.2 para 6 [dcl.enum]  mentions:
An enumeration whose underlying type is fixed is an incomplete type from its point of declaration to immediately after its enum-base (if any), at which point it becomes a complete type. An enumeration whose underlying type is not fixed is an incomplete type from its point of declaration to immediately after the closing } of its enum-specifier, at which point it becomes a complete type.


**What happens instead?**
g++ (GCC) compilation fails:
`
<source>:7:23: error: different underlying type in enum 'enum color<T>::city' [-Wtemplate-body]
    7 | enum color<T>::city : T {
      |                       ^
<source>:3:17: note: previous definition here
    3 |     enum city : int;
      | ^~~
Compiler returned: 1
`


Clang compiles successfully without any error or warning.

A forward-declared scoped enum inside a class template fixes the underlying type to int, but Clang accepts a later definition that uses the template parameter as the underlying type when compiled under -std=c++17.
This must be fixed.

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

Reply via email to