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.

1
  • 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?). Commented Mar 20, 2021 at 15:09

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I will make sure to check out the buglists before posting this kind of questions in the future.
They are great sources but not always the easiest to search. You're welcome.
Just for completeness in case people find themselves here, this is the bug report for LLVM: #50055. It will be fixed in Clang 16.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.