2

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.

Baz
  • 12,713
  • 38
  • 145
  • 268
  • 3
    Since you ask for "pitfalls", note that std::vector is broken in the first place. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2160.html. For a discussion on alternatives see http://stackoverflow.com/questions/670308/alternative-to-vectorbool. – Daniel Daranas May 22 '13 at 09:13

2 Answers2

2

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

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

Alex B
  • 82,554
  • 44
  • 203
  • 280