使用 boost::flyweightstruct T {} 内部(即递归享元)

发布于 2024-10-08 12:04:54 字数 551 浏览 0 评论 0原文

我试图定义一个不可变的文件路径值类型,利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-10-15 12:04:54

此示例展示了如何使用以下方法将 Boost.Flyweight 与递归数据结构结合起来: Boost.Variantboost::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文