C++ Logo


Advanced search

User-defined conversion for strongly typed enums

From: Przemek Kozioł <przemkoz85_at_[hidden]>
Date: Sat, 4 Apr 2020 22:17:03 +0200

Strongly typed enums provide type safety.

I believe, however, the programmer should have an option to define explicit
conversions to the desired types.

Legacy enums are often used to define a set of integer constants, but
because they are unscoped, name conflicts arise. One can use namespaces,
but not inside a class, so one's forced to use something like :

class X {
    struct ID {
        enum {
            NAME = 0x0,
            SIZE = 0x2,

This proposal would combine the best of both worlds: scope and type safety
by default,
while still giving an option for implicit conversion.


Received on 2020-04-04 15:20:10