1

我知道这不是默认的,也可能不是使用 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)
} 

如果可能的话,我希望 tham 可以自动恢复(简单的.get()not .get<T>

似乎它可以存储指向函数的指针(主要是我想使用它)。但我无法从中获取它们(所以我想知道如何将指针存储在 Boost 属性树中以便自动恢复?

4

1 回答 1

2

我将尝试在这里回答问题的要点。我想您可能也对 Boost Serialization 感到满意?

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

  • 纯文本
  • XML
  • 二进制档案

它完全支持各种数据类型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 可以是任何可调用类型

  • 功能类型
  • 函数指针
  • 函数对象(例如std::less()
  • 一个 lambda 表达式

然而,重要的是,按名称注册可调用类型的每个实例,因为地址不会是单独实例的 sam。


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

于 2011-12-01T23:42:01.413 回答