Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no enumeration concept in C++ 20?

I am trying to update some older code to the coming C++20 language using current GCC10.

I was not able to update some templates because they used a std::enable_if<std::is_enum_v<T>> condition and there is no std::enumeration concept defined in C++20.

Of course I can define my own concept but still it makes me wonder why C++20 does not implement all existing type traits as concepts.

Why is this? Did I miss something important?

like image 850
Silicomancer Avatar asked May 18 '20 17:05

Silicomancer


People also ask

Does C have enums?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Can C++ enum class have methods?

No, it cannot. See here. @octavian Note my answer and rethink about your use cases please!

What number do enums start at?

Enum Values The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member.

What is enum in C with example?

In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.


1 Answers

There is no enumeration concept because there was never a specific intent to create a concept to match every type trait. Or even most of them.

The C++20 concept library was, more or less, based on what was needed to make the Ranges library work. It focuses on things concepts are good at, like asking "what can I do with this type?" You can ask if you can default-construct a T or copy/move one, and such. You're not constraining things on what a type is, but what you're going to do with it.

A concept like enumeration just isn't a very useful constraint. Remember: from a logical perspective, if a template constrains a parameter, then it should only use that parameter in accord with that constraint. So if you have a function that takes a default-constructible T, then that function should only use T by default constructing one (though nothing syntactically stops you from violating that, it's generally considered poor form).

To be integral or floating_point requires that the type be one of those fundamental types, which brings with it all of the expressiveness that those types provide.

But what could you do with enumeration by itself? You can default-construct one (though whether this represents a legit enum value or not isn't apparent, so it's not clear what you'd do with it), you can assign and compare it (though even comparison can be overloaded/deleted), you can play games with its underlying type. But that's about it.

If some T is an enumeration, that doesn't mean that the T has any particular enum fields. So the primary feature of an enumeration is lost to you if your constraint is simply that it is an enumeration.

That doesn't mean it can't come up. But just because a constraint occasionally comes up doesn't mean it rises to the level of being a concept.

like image 195
Nicol Bolas Avatar answered Oct 19 '22 13:10

Nicol Bolas