Baz
Baz

Reputation: 13135

Adding std::vector<bool> to a Variant of Containers

I'm wrapping a C interface which has a load function returning a Value* object, which points to a dynamic array of Value objects:

typedef struct Value {
    union {
        int8_t   i8;
        int16_t  i16;
        int32_t  i32;
        int64_t  i64;
        bool     b;
    } value;
} Value_T;

The objects in a given array are always of the same type.

My idea is to represent this as follows in C++:

typedef boost::variant<std::vector<bool>, std::vector<int8_t>, std::vector<int16_t>, std::vector<int32_t>, std::vector<int64_t>, std::vector<std::string> > Container;

Is this reasonable and what pitfalls should I be aware of? Can there be compiler specific issues regarding how bool is defined? I realize that std::vector is represented internally using bits and that there are additional issues in this regard.

I'm working with C++98 compilers.

Upvotes: 2

Views: 180

Answers (2)

Alex B
Alex B

Reputation: 84822

boost::variant is type-agnostic, and should work regardless of the details of std::vector<bool> implementation.

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 473437

Since you're already using Boost, it's best to just use boost::containers::vector<bool>. That container will have the behavior you actually want.

Upvotes: 2

Related Questions