C ++ index variables that are implicitly converted to integers

I have a question about various forms enumand enum class, in particular, to indicate a large number of constants (copied!).

I was wondering if there is a way to declare an enumerated area that is implicitly converted to an integer. To do this, it would be necessary to indicate the addresses of the registers and have access to them with MY_REGISTERS::FOO.

Here are the parameters that I know and have encountered, imagine that there is a function with a signature void do_something(uint32_t bla).

1: enum class

The enum class has scope, but is not implicitly converted to an integer. I consider it important that I do not have to static_castinteger it, so this does not seem to fit correctly.

enum class Foo : uint32_t
{
    BAR = 0x0000,
    BAZ = 0x0001
};

do_something(Foo::BAR) // Illegal, I'd have to `static_cast` here

2: enum

C , ( ). , :

enum Foo : uint32_t
{
    BAR = 0x0000,
    BAZ = 0x0001
}

do_something(Foo::BAR) // Legal, and what I am looking for
do_something(BAR)      // Legal, whilst I don't want this to be possible

3: namespaced enum

, , . - ,

namespace Foo
{
    enum dontcare : uint32_t
    {
        BAR = 0x0000,
        BAZ = 0x0001
    };
}

do_something(Foo::BAR) // Legal, and what I am looking for
do_something(BAR)      // Illegal, just like I want it to be

4: namespaced static constexpr

, , ( enum) , .

namespace Foo
{
    static constexpr uint32_t BAR = 0x0000,
    static constexpr uint32_t BAZ = 0x0001
}

do_something(Foo::BAR) // Legal, and what I am looking for
do_something(BAR)      // Illegal, just like I want it to be

, , , namespaced enum, namespaced constexpr s?

+6
1

, struct enum (Edit: , ):

struct Foo{
    enum: uint32_t
    {
        BAR = 0x0000,
        BAZ = 0x0001
    };
};

Live Demo

++ 11 ([dcl.enum]), , , , Foo. , , :

do_something(Foo::BAR);
+2

All Articles