使用 boost::flyweightstruct T {} 内部(即递归享元)
我试图定义一个不可变的文件路径值类型,利用 boost::flyweight 来共享路径组件。是这样的:
struct filepath_data;
typedef boost::flyweight<filepath_data> filepath;
struct filepath_data {
boost::optional<filepath> parent;
std::string name;
};
当然,这看起来像一个递归结构,但是 boost::flyweight
实际上(本身)并不包含 T
的副本,只是一个 T
的句柄,可以在适当的持有者中查找,所以我认为这个结构应该有效。
不幸的是,它无法编译,因为当 g++ 遇到 typedef 时,它会抱怨 filepath_data 不完整。
所以,问题是,我可以利用 boost::flyweight
的灵活性和更高级的模板参数来使这个结构起作用吗?如果可以,如何实现?
I'm trying to define an immutable file-path value type, taking advantage of boost::flyweight to share path components. Something like this:
struct filepath_data;
typedef boost::flyweight<filepath_data> filepath;
struct filepath_data {
boost::optional<filepath> parent;
std::string name;
};
Of course, this looks like a recursive structure, but boost::flyweight<T>
doesn't actually (itself) contain a copy of T
, just a handle to a T
which can be looked up in the appropriate holder, so I think this structure should work.
Unfortunately, it doesn't compile, because when g++ hits the typedef it complains that filepath_data is incomplete.
So, the question is, can I make use of the flexibility and more advanced template arguments for boost::flyweight<>
to make this structure work, and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此示例展示了如何使用以下方法将 Boost.Flyweight 与递归数据结构结合起来: Boost.Variant 和
boost::recursive_wrapper
。也许您可以使用类似的方法来解决您的问题。This example shows how to combine Boost.Flyweight with a recursive data structure using Boost.Variant and
boost::recursive_wrapper
. Maybe you can use a similar approach for your problem.