You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
C++11 enums don’t work as integer constants as expected #5554
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.
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++."
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" ?
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.
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.
changed the title [-][Feature & Bug Repor] Add support for Scoped Enum (`enum class`)[/-][+]C++11 enums don’t work as integer constants as expected[/+]on Aug 16, 2023
Activity
jeremyong commentedon Aug 5, 2023
Just my 2c, but I think
enum classas 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 commentedon Aug 6, 2023
would you care to elaborate on the "much to be desired" ?
jeremyong commentedon Aug 6, 2023
Sure. As used currently, people often declare an
enum classnot specifically for strong-typing, but in order to get scoped enum field declarations. In exchange, you lose the ability to easily useenum classfields as array indexing operators, bit flags, or myriad other use cases. You can't provide a conversion operator forenum classtypes 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 (bothenum classandenum structmap 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 regularenumfields have proper lexical scoping first.devshgraphicsprogramming commentedon Aug 6, 2023
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.
thats kinda why we have a
core::bitflag<>in our engine to deal with all that.llvm-beanz commentedon Aug 15, 2023
HLSL 2017 and later all support C++11-style
enum classdeclarations, 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 classdeclaration.devshgraphicsprogramming commentedon Aug 16, 2023
yeah when you change the
enumtoclassadd 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 commentedon Aug 16, 2023
I’ve transferred this issue over to DXC because this is a bug not a language feature request.
[-][Feature & Bug Repor] Add support for Scoped Enum (`enum class`)[/-][+]C++11 enums don’t work as integer constants as expected[/+]devshgraphicsprogramming commentedon May 16, 2024
hmm it works with correct
static_castsubstitute of a C-style casthttps://godbolt.org/z/M6Kna6r7s
The only annoying thing is that DXC doesn't recognize
enum classas being something you can template onhttps://godbolt.org/z/8hqrj1ezr
devshgraphicsprogramming commentedon Jul 9, 2024
btw also seems that concepts like
integral_constant<Enum,Enum Val>are bustedhttps://godbolt.org/z/EGaesxvE1
enum classis not a constexpr #6706 as a duplicate of this issuellvm-beanz commentedon Feb 13, 2026
Oops... wrong window. Sorry for the false hope.