[Development] API style guide: scoped enum or not?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu May 4 17:57:46 CEST 2023


Il 04/05/23 15:51, Sune Vuorela ha scritto:
> 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.
> 
> 
> There are definitely more.

There's little reason why registerEventType returns `int` and not 
QEvent::Type, by the way. Model roles could become scoped enumerations, 
which can be explicitly constructed from integers (all of this just to 
retain the switch()-ability -- otherwise a model role should be 
represented by a wrapper type around integers. Certainly not be kept as 
a plain int).

My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4244 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20230504/713e10e0/attachment.bin>


More information about the Development mailing list