C++ Boost.Bind返回类型

C++ Boost.Bind返回类型,c++,boost,boost-bind,c++03,boost-propertytree,C++,Boost,Boost Bind,C++03,Boost Propertytree,我试图用boost.Assign填充boost::property_tree::ptree。因此,我得到了以下工作良好: namespace bpt = boost::property_tree; bpt::ptree pt; boost::assign::make_list_inserter (boost::bind(&bpt::ptree::put<std::string>, pt, _1, _2)) ("one.two.three&qu

我试图用boost.Assign填充
boost::property_tree::ptree
。因此,我得到了以下工作良好:

namespace bpt = boost::property_tree;
bpt::ptree pt;
boost::assign::make_list_inserter
    (boost::bind(&bpt::ptree::put<std::string>, pt, _1, _2))
        ("one.two.three", "four")
        ("one.two.five", "six");
名称空间bpt=boost::属性树;
bpt::ptree-pt;
boost::assign::生成列表\u插入器
(boost::bind(&bpt::ptree::put,pt,_1,_2))
(“一、二、三、四”)
(“一、二、五”、“六”);
但是,当我试图使这段代码看起来更好时,它无法编译:

typedef bpt::ptree& (bpt::ptree::*PutType)
    (const bpt::path_of<std::string>::type&, const std::string &);

PutType Put = &bpt::ptree::put<std::string>;

inline boost::assign::list_inserter<PutType> put(bpt::ptree &pt) {
  PutType putFunction = boost::bind(Put, pt, _1, _2); // !!! compile error
  return boost::assign::make_list_inserter(putFunction);
}

//and use it like:
put(pt)
    ("one.two.three", "four")
    ("one.two.five", "six");
typedef bpt::ptree&(bpt::ptree::*PutType)
(常量bpt::path_of::type&,常量std::string&);
PutType Put=&bpt::ptree::Put;
内联boost::assign::list_插入器put(bpt::ptree&pt){
PutType putFunction=boost::bind(Put,pt,_1,_2);/!!!编译错误
返回boost::assign::make_list_inserter(put函数);
}
//然后像这样使用它:
put(pt)
(“一、二、三、四”)
(“一、二、五”、“六”);
错误消息是

SomeFile.cpp:在函数“boost::assign::list_inserter put(boost::property_tree::ptree&)”中:

SomeFile.cpp:42:错误:无法在初始化中将“boost::\u bi::bind\u t”转换为“boost::property\u tree::ptree&(boost::property\u tree::ptree::*)(const boost::property\u tree::string\u path&,const std::string&)”


使代码正常工作的最佳方法是什么?

代码中有几个错误:

  • boost::bind()
    按值存储绑定的参数,以便
    boost::bind(&bpt::ptree::put,pt,_1,_2)
    复制并填充该副本。而是传递一个指针:
    boost::bind(&bpt::ptree::put,&pt,\u 1,\u 2)
    或使用
    boost::ref(pt)
  • boost::bind
    返回的对象不能转换为指针类型,这就是为什么
    PutType putFunction=boost::bind(Put,pt,_1,_2)无法编译

  • 在没有
    auto
    关键字的C++03中,您无法轻松捕获
    boost::bind
    boost::list\u inserter
    的类型。您可以将这两种方法的结果都打包到
    boost::function
    中,但在我看来,这样做太麻烦了

    但在C++03中,您可以通过以下简单方式实现所需的语法:

    namespace bpt = boost::property_tree;
    
    struct PtreeInserter
    {
        bpt::ptree* pt;
    
        PtreeInserter(bpt::ptree& pt) : pt(&pt) {}
    
        template<class A1, class A2>
        PtreeInserter const& operator()(A1 a1, A2 a2) const {
            pt->put(a1, a2);
            return *this;
        }
    };
    
    int main() {
        bpt::ptree pt;
        typedef PtreeInserter put;
        put(pt)
            ("one.two.three", "four")
            ("one.two.five", "six");
    }
    
    名称空间bpt=boost::属性树;
    结构PtreeInserter
    {
    bpt::ptree*pt;
    PtreeInserter(bpt::ptree&pt):pt(&pt){}
    模板
    PtreeInserter常量和运算符()(A1 A1、A2 A2)常量{
    pt->put(a1,a2);
    归还*这个;
    }
    };
    int main(){
    bpt::ptree-pt;
    typedef PTREINSERTER put;
    put(pt)
    (“一、二、三、四”)
    (“一、二、五”、“六”);
    }
    
    谢谢(+1)。你们知道,我怎样才能重写代码使它工作吗?再次感谢你们。为什么
    boost::function
    很难处理?是否存在一些性能损失?@Loom
    boost::function
    在构造和复制时涉及内存分配;和指针间接调用时。它针对一些简单的情况进行了优化,以避免内存分配,但这些情况并非如此。