gpt4 book ai didi

c++ - 如何转发声明 boost::ptree::iterator

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:05 26 4
gpt4 key购买 nike

我想在我的项目中使用 boost ptree,但由于 ptree.hpp 导致包含另外 1000 个头文件,这大大增加了编译时间(例如从 1 秒到 7 秒),并且因为它在 20 多个不同的 cpp 文件中需要这是 Not Acceptable (预编译的 header 不会改善太多)。所以我正在考虑将boost ptree封装在我自己的类中,比如

// myptree.h
#include <boost/property_tree/ptree_fwd.hpp>

class myptree {
private:
boost::property_tree::ptree *m_tree;

public:
...
// adding new (single value) members to the the tree
void put(const std::string&, double);
void put(const std::string&, int);
void put(const std::string&, const std::string&);

// returning (single value) members of the tree
double get_double(const std::string&) const;
int get_int(const std::string&) const;
std::string get_str(const std::string&) const;

// working with subtrees
void push_back(const std::string&, const myptree&);
myptree get_child(const std::string&) const;

// import/export
void read_from_json(const std::string&);
void write_to_json(const std::string&) const;

};

但是,我没能很好地实现迭代器。理想情况下,我希望有一个 boost::property_tree::ptree::iterator 作为私有(private)成员变量,然后可以使用我自己的成员函数在 m_tree 上迭代,但是据我了解How do I forward declare an inner class?这通常是不可能的。在此类中实现迭代器的任何优雅方法?

最佳答案

您的问题很适合 Pimpl idiom ,又名编译器防火墙,又名Handle-Body。另见 article .您提出的解决方案与该成语非常相似。

要对您的客户端隐藏ptree 的迭代器,请查看this article 中介绍的any_iterator 技术。 .

您可以找到 any_iterator 的实现 herehere .

关于c++ - 如何转发声明 boost::ptree::iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5970899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com