【问题标题】:Adding std::vector<bool> to a Variant of Containers将 std::vector<bool> 添加到容器变体
【发布时间】:2013-05-22 09:10:14
【问题描述】:

我包装了一个 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 编译器。

【问题讨论】:

标签: c++ boost c++98


【解决方案1】:

由于您已经在使用 Boost,因此最好只使用 boost::containers::vector&lt;bool&gt;。该容器将具有您真正想要的行为。

【讨论】:

    【解决方案2】:

    boost::variant 与类型无关,无论std::vector&lt;bool&gt; 实现的细节如何,都应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多