Bug 49224 - [C++0x] Scoped enumeration instantiated even if not required
Summary: [C++0x] Scoped enumeration instantiated even if not required
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 86183 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-05-29 14:19 UTC by Johannes Schaub
Modified: 2021-08-05 09:59 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-08-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Johannes Schaub 2011-05-29 14:19:19 UTC
GCC should not instantiate the definition of the scoped enumeration:

template<typename T>
struct A {
  enum class B {
    X = T::value
  };
};

int main() {
  A<int> a;
}

GCC error:

main1.cpp: In instantiation of ‘A<int>’:
main1.cpp:9:10:   instantiated from here
main1.cpp:3:14: error: ‘value’ is not a member of ‘int’

This code looks well-formed. Only if we look into the enumeration, as
"A<int>::B::X", the definition of the enumeration is required to exist and thus
implicitly instantiated. This is specified at 14.7.1p1 and p2.
Comment 1 Eric Gallager 2017-08-18 13:39:38 UTC
(In reply to Johannes Schaub from comment #0)
> GCC should not instantiate the definition of the scoped enumeration:
> 
> template<typename T>
> struct A {
>   enum class B {
>     X = T::value
>   };
> };
> 
> int main() {
>   A<int> a;
> }
> 
> GCC error:
> 
> main1.cpp: In instantiation of ‘A<int>’:
> main1.cpp:9:10:   instantiated from here
> main1.cpp:3:14: error: ‘value’ is not a member of ‘int’
> 
> This code looks well-formed. Only if we look into the enumeration, as
> "A<int>::B::X", the definition of the enumeration is required to exist and
> thus implicitly instantiated. This is specified at 14.7.1p1 and p2.

Confirmed that g++ rejects it, but clang rejects it too:

$ /usr/local/bin/g++ -c -Wall -Wextra -pedantic 49224.cc
49224.cc: In instantiation of ‘struct A<int>’:
49224.cc:9:9:   required from here
49224.cc:3:13: error: ‘value’ is not a member of ‘int’
  enum class B {
             ^
49224.cc: In function ‘int main()’:
49224.cc:9:9: warning: unused variable ‘a’ [-Wunused-variable]
  A<int> a;
         ^
$ /sw/opt/llvm-3.1/bin/clang++ -c -Wall -Wextra -pedantic 49224.cc
49224.cc:3:7: error: expected identifier or '{'
        enum class B {
             ^
49224.cc:3:2: warning: declaration does not declare anything [-Wmissing-declarations]
        enum class B {
        ^~~~
49224.cc:9:9: warning: unused variable 'a' [-Wunused-variable]
        A<int> a;
               ^
2 warnings and 1 error generated.
$

Since I'm not sure if this is intentional or not, I'm leaving it UNCONFIRMED.
Comment 2 Eric Gallager 2017-08-18 13:44:23 UTC
Actually scratch that, my version of clang is old so it doesn't default to c++11. When I manually force -std=c++0x, clang accepts the code, while g++ still rejects it:

$ /usr/local/bin/g++ -c -Wall -Wextra -pedantic -std=c++0x 49224.cc
49224.cc: In instantiation of ‘struct A<int>’:
49224.cc:9:9:   required from here
49224.cc:3:13: error: ‘value’ is not a member of ‘int’
  enum class B {
             ^
49224.cc: In function ‘int main()’:
49224.cc:9:9: warning: unused variable ‘a’ [-Wunused-variable]
  A<int> a;
         ^
$ /sw/opt/llvm-3.1/bin/clang++ -c -Wall -Wextra -pedantic -std=c++0x 49224.cc
49224.cc:9:9: warning: unused variable 'a' [-Wunused-variable]
        A<int> a;
               ^
1 warning generated.
$

So confirming then.
Comment 3 Eric Gallager 2018-08-30 02:22:44 UTC
cc-ing c++ FE maintainers
Comment 4 Andrew Pinski 2021-08-05 09:59:45 UTC
*** Bug 86183 has been marked as a duplicate of this bug. ***