Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top