Boost::variant成员子集的另一个Boost::variant
boost::variant members subset of another boost::variant
我正在使用一个相当笨拙的c接口来存储集合。类LowLevelStorer
表示我为这个接口编写的包装器。Storer
类是与Data
相关的高级类。它进行缓存并将数据捆绑到只有LowLevelStorer
才知道的更复杂的数据类型中。我的代码的其余部分只关注Data
,而不了解LowLevelData
。
在下面的示例代码中,我希望Data
变体中的成员包含在LowLevelData
变体中。有没有一种方法可以说明我是怎么做的?
我真的不明白的是为什么下面的代码编译,事实上,为什么它实际上是正确的工作。也就是说,void operator()(const SimplePath&, const Data& data) const
接受一个Data引用,但在调用void operator()(const LowLevelData& data) const
时似乎正确地将其转换为LowLevelData对象。这怎么可能呢?
在我的数据对象的幕后是否发生了大量的复制?
#include "boost/variant.hpp"
#include "boost/variant/apply_visitor.hpp"
#include <vector>
class Complex{};
typedef boost::variant< std::vector<Complex>, std::vector<int>, std::vector<std::string> > LowLevelData;
class LowLevelStorer
{
public:
LowLevelStorer(): _storeVisitor(StoreVisitor()){}
void operator()(const LowLevelData& data) const
{
boost::apply_visitor(_storeVisitor, data);
}
private:
class StoreVisitor: public boost::static_visitor<>
{
public:
void operator()(const std::vector<Complex>&) const {}
void operator()(const std::vector<int>& i) const {}
void operator()(const std::vector<std::string>&) const {}
};
StoreVisitor _storeVisitor;
};
struct SimplePath{};
struct BundlePath{};
typedef boost::variant< SimplePath, BundlePath > Path;
typedef boost::variant< std::vector<std::string>, std::vector<int> > Data;
class Storer
{
public:
Storer(const LowLevelStorer& lowLevelStorer): _converter(Converter(lowLevelStorer)){}
void operator()(const Path& path, const Data& data) const
{
boost::apply_visitor(_converter, path, data);
}
private:
class Converter: public boost::static_visitor<>
{
public:
Converter(const LowLevelStorer& lowLevelStorer): _lowLevelStorer(lowLevelStorer){}
void operator()(const SimplePath&, const Data& data) const {
_lowLevelStorer(data);
}
void operator()(const BundlePath&, const Data& data) const {
_lowLevelStorer(std::vector<Complex>());
}
private:
const LowLevelStorer _lowLevelStorer;
};
const Converter _converter;
};
int main()
{
Storer storer((LowLevelStorer()));
std::vector<int> v;
v.push_back(13);
storer(Path(SimplePath()),v);
return 0;
}
我怀疑,当您提供参数作为另一个参数时,它使用一个变量的原因是因为您的两个变量具有所有公共类型,这使得您的变量可以相互转换。
我认为对于所有三种类型只使用一种变体而完全跳过另一种变体是完全可以的,因为第二种变体仅仅是第一种类型的子集。
相关文章:
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- boost::variant - 如何分配值
- boost::variant - 对变体应用算术的最简单方法
- Boost Variant 是否提供与 std holds_alternative类似的功能?
- 为什么Boost Variant使用模板构造函数而不是Boost::beast::websocket::stream的移
- SWIG and Boost::variant
- "非静态数据成员之前需要构造函数" - 我是否使用"boost::variant"
- boost::variant 当 bool 显示为可能的类型时,会给出错误的结果
- 使用 boost::variant 并获取泛型返回类型
- 使用 boost::variant 调用模板类函数
- boost::variant 是否仍用于野外的新项目?
- 如何实现 boost::variant 派生类
- decltype 和 boost::variant - 检索当前值
- QVariant vs boost::any vs boost::variant
- 使用Boost :: black在boost :: variant中使用时,请警告编译器
- Getter for boost::variant object
- 有关使用 boost::variant 重新加载运算符的编译错误
- boost::variant:具有递归向量类型的奇怪行为
- boost::variant-获取成员的向量属性