将 std::vector<bool> 添加到容器的变体中

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

本文关键字:gt vector std lt bool 添加      更新时间:2023-10-16

我正在包装一个C接口,它有一个加载函数返回一个Value*对象,它指向Value对象的动态数组:

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

给定数组中的对象总是同一类型。

我的想法是在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;

这是合理的吗?我应该注意哪些陷阱?是否有编译器特定的问题,关于如何定义bool ?我意识到std::vector在内部是用位表示的,这方面还有其他问题。

我正在使用c++ 98编译器

既然你已经在使用Boost,最好只使用boost::containers::vector<bool>。这个容器会有你想要的行为

boost::variant是类型无关的,无论std::vector<bool>实现的细节如何,它都应该工作。