Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++11 enums don’t work as integer constants as expected #5554

Open
devshgraphicsprogramming opened this issue Aug 5, 2023 · 9 comments
Open
Labels
bug Bug, regression, crash hlsl2021 Pertaining to HLSL2021 features
Milestone

Comments

@devshgraphicsprogramming

Is your feature request related to a problem? Please describe.

Thanks to HLSL2021 I can share enumeration declarations between C++ and HLSL202x.

Bug Report: However whenever I declare the enum class HLSL does not complain until I actually try to use the scoped enum. Seems that enum class passes through your tokenizer unharmed, but then the rest of the logic is gone.

Describe the solution you'd like

For the compiler to support scope enum lookup enum_t:: without errors.

Describe alternatives you've considered

Not using scoped enums but a lot of my C++ code I want to expose to HLSL already does, so not fun.

Additional context

Here's a repro:
https://godbolt.org/z/1TdYh9E1a

@jeremyong
Copy link

Just my 2c, but I think enum class as a feature in C++ leaves much to be desired and don't know that the goal of future HLSL design should be driven primarily by the metric of "code shareability with C++."

@devshgraphicsprogramming
Copy link
Author

Just my 2c, but I think enum class as a feature in C++ leaves much to be desired and don't know that the goal of future HLSL design should be driven primarily by the metric of "code shareability with C++."

would you care to elaborate on the "much to be desired" ?

@jeremyong
Copy link

Sure. As used currently, people often declare an enum class not specifically for strong-typing, but in order to get scoped enum field declarations. In exchange, you lose the ability to easily use enum class fields as array indexing operators, bit flags, or myriad other use cases. You can't provide a conversion operator for enum class types to circumvent this issue either, so you end up resorting to a soup of macros or nasty infectious templates to provide that behavior. The syntax itself is non-obvious (both enum class and enum struct map to the same type of declaration with no distinction, but my god what an awful naming scheme that is). I think we're better off making regular enum fields have proper lexical scoping first.

@devshgraphicsprogramming
Copy link
Author

In exchange, you lose the ability to easily use enum class fields as array indexing operators, bit flags, or myriad other use cases.

you can use them as bit flags, you just need to declare the bitwise operators for them.

I think its great because it stops you "accidentally" bitwise-op'ing unrelated bitflags.

You can't provide a conversion operator for enum class types to circumvent this issue either, so you end up resorting to a soup of macros or nasty infectious templates to provide that behavior.

thats kinda why we have a core::bitflag<> in our engine to deal with all that.

@llvm-beanz
Copy link
Collaborator

llvm-beanz commented Aug 15, 2023

HLSL 2017 and later all support C++11-style enum class declarations, and the accessing seems to work for me (Compiler Explorer Link).

Can you provide an example of what doesn't work for you? The link you provided doesn't actually have an enum class declaration.

@devshgraphicsprogramming
Copy link
Author

yeah when you change the enum to class add scope the enums in the example I gave then it starts complaining about not being able to use a scoped enum as as integral expression (whereas it uses an unscoped one just fine)
https://godbolt.org/z/Pxd1zacr7

@llvm-beanz llvm-beanz transferred this issue from microsoft/hlsl-specs Aug 16, 2023
@llvm-beanz llvm-beanz added the bug Bug, regression, crash label Aug 16, 2023
@llvm-beanz
Copy link
Collaborator

I’ve transferred this issue over to DXC because this is a bug not a language feature request.

@llvm-beanz llvm-beanz changed the title [Feature & Bug Repor] Add support for Scoped Enum (enum class) C++11 enums don’t work as integer constants as expected Aug 16, 2023
@llvm-beanz llvm-beanz added the hlsl2021 Pertaining to HLSL2021 features label Sep 6, 2023
@devshgraphicsprogramming
Copy link
Author

hmm it works with correct static_cast substitute of a C-style cast
https://godbolt.org/z/M6Kna6r7s

The only annoying thing is that DXC doesn't recognize enum class as being something you can template on
https://godbolt.org/z/8hqrj1ezr

@devshgraphicsprogramming
Copy link
Author

btw also seems that concepts like integral_constant<Enum,Enum Val> are busted
https://godbolt.org/z/EGaesxvE1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug, regression, crash hlsl2021 Pertaining to HLSL2021 features
Projects
Status: Triaged
Development

No branches or pull requests

3 participants