[Development] API style guide: scoped enum or not?
Marc Mutz
marc.mutz at qt.io
Thu May 4 17:27:21 CEST 2023
On 04.05.23 15:51, Sune Vuorela wrote:
> On 2023-05-04, Marc Mutz via Development <development at qt-project.org> wrote:
>> that keeps unported code running. The main issue isn't the scoping, the
>> main issue will be the missing implicit conversion to underlying_type.
>
> In few cases the implicit conversion to underlying_type is kind of important.
>
> Especially in the cases where the api has int and is mostly used with
> enums or is somehow user extendable.
>
> Qt::ItemDataRole is one of them that comes to mind.
>
> switch(role) {
> case Qt::UserRole+1:
> ....
>
> QEvent::Type is another one where one registerEventType, recieves an int
> and then force-cast it to QEvent::Type and still compares back to that
> int in other places.
The int is the problem, not the enum. With C++17, scoped enums have a
kind of explicit ctor from underlying_type, so at least that direction
is reasonably convenient. For the other direction, there's
std::to_underlying/qToUndelying().
So, enum-backed APIs taking int will have to be ported to take the enum
instead. On the plus side, you get type-richer and safer APIs.
Thanks,
Marc
--
Marc Mutz <marc.mutz at qt.io>
Principal Software Engineer
The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany
www.qt.io
Geschäftsführer: Mika Pälsi, Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht Charlottenburg,
HRB 144331 B
More information about the Development
mailing list