2

I am trying to use enumerations of std::vector<bool> because i'd like to represent some values as a vector of bits.

So I tried the following code:

enum class ProximityStateEnum : std::vector<bool> {

    Unknown = std::vector<bool>{false,false},

    NotConnected = std::vector<bool>{false,true},

    Connected = std::vector<bool>{true,false},

    ConnectedButNotLatched = std::vector<bool>{true,true}

};

But when I compile the code with this, I get the error underlying type ‘std::vector<bool>’ of ‘ProximityStateEnum’ must be an integral type. How can I do to create an enum of vectors ?

3 Answers 3

4

enum and enum class are not meant as aliases to instances of arbitrary objects - they're limited to integral types, and every explicit value must be a constant expression. See the cppreference page on enum for more information.

If you want to give a name to particular instances of std::vector<bool>, either use functions or inline variables:

auto Unknown()                { return std::vector<bool>{false,false}; }
auto NotConnected()           { return std::vector<bool>{false,true}; }
auto Connected()              { return std::vector<bool>{true,false}; }
auto ConnectedButNotLatched() { return std::vector<bool>{true,true}; }

// ...or, in C++17:
inline auto Unknown =                std::vector<bool>{false,false};
inline auto NotConnected =           std::vector<bool>{false,true};
inline auto Connected =              std::vector<bool>{true,false};
inline auto ConnectedButNotLatched = std::vector<bool>{true,true};

In your particular use case, you don't really need an std::vector because you know the size of your bitset in advance - std::bitset or binary literals will work well:

enum class ProximityStateEnum : /* some suitable type */ 
{
    Unknown =                0b00,
    NotConnected =           0b01,
    Connected =              0b10,
    ConnectedButNotLatched = 0b11
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Indeed, std::bitset could also be usable but I previously decided to stick with vectors so now I can't really change. Your approach without the enum is interesting, but as I need to group my different values, I think I'd make them static members of structs
4

You cannot; enums can only be based on integral types.

If it can be helpful, since C++14 you can use binary literals to specify their values:

enum class ProximityStateEnum {
    Unknown = 0b00,
    NotConnected = 0b01,
    Connected = 0b10,
    ConnectedButNotLatched = 0b11
};

which is pretty much as clear as your idea, but way more compact and orders of magnitude more efficient than any hypothetical std::vector based solution.

Another possibility that is often used in enums where particular bits have particular meaning is to build them with expressions:

enum ProximityStateEnum {
    Unknown = 0b00,
    NotConnected = 0b01,
    Connected = 0b10,
    ConnectedButNotLatched = Connected | NotConnected
};

3 Comments

Thanks for this answer. In my case, I really need the type to be of type std::vector<bool>. i will probably take a look at struct with static members.
@Xatyrian: sounds like an XY problem, you should probably describe why exactly you need it to be an std::vector<bool>.
Also, a struct with only static members doesn't make much sense, use a namespace.
0

As said by other, enums can only be integral types.

If the lenght of your vectors of bool is constant (as in you example, where is 2), you can substitute the std::vector<bool> with a std::array<bool, length>. One advantage in using std::array instead of std::vector is that you can define it as constexpr.

So a reasonable (IMHO) alternative is define a enum type based on std::size_t, with sequential values starting from 0,

enum ProximityStateEnum : std::size_t
 {
    Unknown = 0U,
    NotConnected,
    Connected,
    ConnectedButNotLatched
 };

and a static constexpr std::array of std::arrays of bool

// .......................... last value of the enum vvvvvvvvvvvvvvvvvvvvvv
static constexpr std::array<std::array<bool, 2U>, 1U+ConnectedButNotLatched>
   pseArr { { { {false, false} }, { {false, true} },
              { {true, false} },  { {true, true} } } };

that you can use as follows

pseArr[NotConnected][0] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.