this post was submitted on 05 Aug 2024
14 points (93.8% liked)

C++

1805 readers
2 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
top 3 comments
sorted by: hot top controversial new old
[–] bitcrafter 2 points 4 months ago (1 children)

Could someone explain to me what the value is in making it really easy to initialize an enum class with a value that is not one of the enumerated values?

[–] [email protected] 1 points 4 months ago* (last edited 4 months ago) (1 children)

Good question. My response would be: only where it is not used as an enum. The example in the article is one example (although I've never seen that idiom in the wild) or for doing things like bitfields (the opposite of the to_underlying section).

[–] bitcrafter 1 points 4 months ago

Ah. In that case, I look forward to there being a new keyword in C++26 or later that you add to enforce that the enumeration really only does take one of the enumerated values, rather than just being a strongly typed number, since that seems to be the way that the language likes to do things...