C++ 自动输入Boost';s属性树
目前我有以下代码:C++ 自动输入Boost';s属性树,c++,boost,boost-propertytree,C++,Boost,Boost Propertytree,目前我有以下代码: if(!variables["width"].defaulted()) { configTree.put(treeNames["width"], variables["width"].as<int>()); } if(!variables["height"].defaulted()) { configTree.put(treeNames["height"], variables["height"].as<int>()); } if(
if(!variables["width"].defaulted())
{
configTree.put(treeNames["width"], variables["width"].as<int>());
}
if(!variables["height"].defaulted())
{
configTree.put(treeNames["height"], variables["height"].as<int>());
}
if(!variables["fullscreen"].defaulted())
{
configTree.put(treeNames["fullscreen"], variables["height"].as<int>());
}
if(!variables[“width”].default())
{
configTree.put(treeNames[“width”],变量[“width”].as());
}
如果(!variables[“height”].default())
{
configTree.put(treeNames[“height”],变量[“height”].as());
}
如果(!variables[“fullscreen”].default())
{
configTree.put(treeNames[“全屏”],变量[“高度”].as());
}
我只是想简单地解释一下。唯一阻止我的是,我将来还会有std::string变量,这意味着简单地遍历所有值并使用as()是行不通的
我曾考虑尝试使用boost::any for as(),但这不起作用(模板错误的负载)。我还考虑过让一个元组的值指定它将是哪种类型,然后通过该值切换并调用适当的as(),但这似乎有点过分了
有没有简单的方法?模板void xput(const char*name){
template<class T> void xput(const char* name) {
if(!variables[name].defaulted())
configTree.put(treeNames[name], variables[name].as<T>());
}
xput<int>("width");
xput<int>("height");
xput<int>("fullscreen");
xput<std::string>("future-option");
如果(!variables[name].defaulted())
configTree.put(treeNames[name],variables[name].as());
}
xput(“宽度”);
xput(“高度”);
xput(“全屏”);
xput(“未来期权”);
如果您想实现运行时多态性,请将上述函数转换为一个functor,并将其分配给boost::function。您想实现什么?(如果您不想实现任何目标,那么很容易简化。只需删除所有内容。)我想检查每个变量值,并将其插入配置树、字符串或int中。