Boost属性树:如何在其中存储指针

Boost Property tree: how to store pointers in it?

本文关键字:在其中 存储 指针 属性 Boost      更新时间:2023-10-16

我知道这不是默认的,也许不是首选的方式来使用Boost属性树。但它似乎都需要创建命名指针树。所以我试了:

#include <boost/property_tree/ptree.hpp>
#include <iostream>
#include <string>
template <class T>
int append(T val)
{
    std::cout << "hello";
    return 0;
}
int main()
{
    using boost::property_tree::ptree;
    ptree pt;
    pt.put("function-int-pointer", &append<int>);
    (pt.get("function-int-pointer", NULL))(123);
    // ^-- error C2064: term does not evaluate to a function taking 1 arguments
    (pt.get<int(*)(int)>("function-int-pointer"))(123);
    // ^-- error C2678: binary '>>' : no operator found which takes a left-hand 
    // operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no 
    // acceptable conversion)
} 

如果可能的话,我希望它是自动恢复的(简单的.get()而不是.get<T>)

它似乎可以存储指向函数的指针(我想使用它的主要原因)。但我不能得到他们从它(所以我想知道如何存储指针在Boost属性树,使他们会自动恢复?

我在这里试着回答这个问题的要点。我想你可能对Boost序列化也很满意吧?

Boost序列化是一个非常强大的通用序列化库,能够序列化到

  • XML><
  • 二进制档案/gh>

它完全支持各种数据类型1 -尽管不支持现成的函数指针。然而,由于它的可扩展特性, Peter Dimov提出了一种按名称序列化函数的方法,手动注册函数。在草图中,它看起来像:

template<class T> void register_static( T * pt, std::string const & name ) 
 { 
 // add to name<->address maps 
 } 
template<class T> void save(T * const & pt) 
 { 
 // look up the name of pt and save 
 } 
template<class T> void load(T * & t) 
 { 
 // load name, look up address, store in pt 
 }
注意T可以是任何可调用类型

    <
  • 函数类型/gh>
  • 函数指针
  • 功能对象(如std::less())
  • 一个lambda表达式
但是,按名称注册可调用类型的每个实例是很重要的,因为单独的实例的地址不会相同。


<子>1标准容器、指针、带有内部(指针)引用的数据结构、别名指针、循环树等。

相关文章: