Boost属性树:如何在其中存储指针
Boost Property tree: how to store pointers in it?
我知道这不是默认的,也许不是首选的方式来使用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标准容器、指针、带有内部(指针)引用的数据结构、别名指针、循环树等。
相关文章:
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 在哪里存储跨平台C++应用存储?
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- "std::string"是否将其字符作为签名字符在内部存储?
- 有没有一种惯用的方法可以在不存储变换或不必要地重新计算的情况下找到数组变换的最小/最大值?
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 我写了一个简单的矢量程序,在其中我得到了以下输出。你能帮我理解它的输出吗?
- 自动完成在git存储库中的Visual Studio项目中不起作用
- 为什么我的数组输出一个不在其中的元素
- 定义在其中执行不同代码路径的代码节
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- 是否可以检索与柯南包管理器一起打包的库的源代码,以便在其中进行调试?
- C :宏参数可以在其中有空间吗?
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 长度()在C 中显示0(尽管字符串中存储在其中)
- 如何将 void 指针类型转换为 int 指针,然后在其中存储 int
- 我如何定义一个向量,并用C++在其中存储对象
- 从文件中获取输入并在其中存储平均值
- Boost属性树:如何在其中存储指针
- 如何在无符号int上使用位标志,以便在其中存储额外的布尔值