0

This is a follow up question of Undefined behavior of constexpr static cast from int to scoped enum with non-fixed underlying type compiles in C++17 (same question for scoped enumerations). TLDR is that scoped enums always have a fixed underlying type (by default int, hence you can always cast from int to scoped enum).

I wonder if the following should or should not compile in C++17

enum E
{
    A, B
};

constexpr E x = static_cast<E>(2);

This compiles with both GCC 9.3.0 and Clang 10.0.0 on Ubuntu 20.04.

My question is

  1. Should this compile?
  2. If it should, why?

See the other post for a detailed reasoning why I think it should not compile for unscoped enums. The same reasoning applies for this question as well.

skalet
  • 365
  • 1
  • 9
  • Please explain why you vote down the question so that I can improve the question. The question is well defined and is not a duplicate of any other. I have done thorough research on the subject (as shown in the other thread), and I am trying to solve an underlying problem that would use the fact that the above code is ill-formed. I choose to not post all information in this question, since I don't want to copy paste too much from the other thread (if that is what you don't like, but what do I know?). – skalet Mar 20 '21 at 15:09

1 Answers1

2

It should not compile. It's a compiler bug. The reason it was made undefined in CWG 1766 was so that this conversion would be diagnosed in constant expressions.

This is GCC bug #95701. I could not find an open bug for Clang.

Jeff Garrett
  • 5,863
  • 1
  • 13
  • 12
  • Thanks! I will make sure to check out the buglists before posting this kind of questions in the future. – skalet Mar 20 '21 at 15:59
  • They are great sources but not always the easiest to search. You're welcome. – Jeff Garrett Mar 20 '21 at 16:09
  • Just for completeness in case people find themselves here, this is the bug report for LLVM: [#50055](https://github.com/llvm/llvm-project/issues/50055). It will be fixed in [Clang 16](https://clang.llvm.org/docs/ReleaseNotes.html#potentially-breaking-changes). – asm Nov 03 '22 at 14:37