C++11 boost-1.55 boost::property_tree::ptree是否使用c++;11?

C++11 boost-1.55 boost::property_tree::ptree是否使用c++;11?,c++11,boost,C++11,Boost,我正在尝试编译以下源代码 #include <boost/property_tree/ptree.hpp> int main() { boost::property_tree::ptree b; b.push_back(std::make_pair("a", "b")); return 9; } 我得到了以下错误 ./source.cpp: In function ‘int main()’: ./source.cpp:5:41: error: no ma

我正在尝试编译以下源代码

#include <boost/property_tree/ptree.hpp>

int main() {
    boost::property_tree::ptree b;
    b.push_back(std::make_pair("a", "b"));

    return 9;
}
我得到了以下错误

./source.cpp: In function ‘int main()’:
./source.cpp:5:41: error: no matching function for call to ‘boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >::push_back(std::pair<const char*, const char*>)’
     b.push_back(std::make_pair("a", "b"));
                                     ^
In file included from /opt/boost/boost_1_55_0/boost/property_tree/ptree.hpp:516:0,
                 from ./source.cpp:1:
/opt/boost/boost_1_55_0/boost/property_tree/detail/ptree_implementation.hpp:362:9: note: candidate: boost::property_tree::basic_ptree<Key, Data, KeyCompare>::iterator boost::property_tree::basic_ptree<Key, Data, KeyCompare>::push_back(const value_type&) [with Key = std::basic_string<char>; Data = std::basic_string<char>; KeyCompare = std::less<std::basic_string<char> >; boost::property_tree::basic_ptree<Key, Data, KeyCompare>::value_type = std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >]
         basic_ptree<K, D, C>::push_back(const value_type &value)
         ^
/opt/boost/boost_1_55_0/boost/property_tree/detail/ptree_implementation.hpp:362:9: note:   no known conversion for argument 1 from ‘std::pair<const char*, const char*>’ to ‘const value_type& {aka const std::pair<const std::basic_string<char>, boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> > >&}’
/source.cpp:在函数“int main()”中:
./source.cpp:5:41:错误:调用“boost::property_tree::basic_ptree::push_back(std::pair)”时没有匹配的函数
b、 向后推(标准:制作成对(“a”、“b”);
^
在/opt/boost/boost_1_55_0/boost/property_tree/ptree.hpp:516:0中包含的文件中,
from./source.cpp:1:
/opt/boost/boost_1_55_0/boost/property_tree/detail/ptree_实现。hpp:362:9:注:候选:boost::property_tree::basic_ptree::iterator boost::property_tree::basic_-tree::push_-back(const-value-type&)[带Key=std::basic_-string;Data=std::basic_-string;Data=std::basic_-string;keycare=std\u-string;keycare=std=std::less;keycompar=std::
基本树::推回(常量值类型和值)
^
/opt/boost/boost_1_55_0/boost/property_tree/detail/ptree_实现。hpp:362:9:注意:参数1从'std::pair'到'const value_type&{aka const std::pair&}的转换未知
注意:如果我在编译时没有--std=c++11,错误就会消失


问题:如何使用c++11标准或更高版本编译它?我已经找到了,但是在boost-1.55中它已经被修复了。提前谢谢你

是的。这里真正的变化是
std::make\u pair

C++11更改了
std::pair
转换规则,以及
make_pair
便利功能,该功能已启用移动感知

分析它 属性树允许如下构造:

ptree pt("data");
构造仅包含值数据而不包含子节点的树。但是,该对的
第二个
成员的间接转换不适用于c++11模式:

std::pair<std::string, ptree> v = std::make_pair("a", "b");
使类型显式始终有助于:

b.push_back(ptree::value_type {"a", "b"});
统一初始化消除了一些缺点:

b.push_back({"a", ptree{"b"}});
为了完整性,非显式构造函数更灵活:

b.push_back({"a", {}});

或者您可以完全避免使用
值类型
)接口

b.add_child("a", ptree{"b"});
b.add_child("a", {}).put_value("b");
但这里真正的关键是值节点不能有子节点。为什么不把价值放在第一位呢

这是我的建议。我觉得
std::pair
是一个实现细节,只对与其他通用代码进行互操作有用。这会损害可读性

现场演示 所有的工作区都幸福地生活在一起

印刷品

<?xml version="1.0" encoding="utf-8"?>
<a>b</a>
<a>b</a>
<a>b</a>
<a/>
<a>b</a>
<a>b</a>
<a>b</a>

B
B
B
B
B
B

如果您有时间进行精彩的演讲,请听Stephan Lavavej解释为什么使用
std::make_pair
不是您想要的解决方案。他还解释了c++11中对
std::pair
std::make_-pair
的一些更改
b.push_back({"a", {}});
b.add_child("a", ptree{"b"});
b.add_child("a", {}).put_value("b");
b.add("a", "b");
<?xml version="1.0" encoding="utf-8"?>
<a>b</a>
<a>b</a>
<a>b</a>
<a/>
<a>b</a>
<a>b</a>
<a>b</a>