Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::绑定并插入boost::无序映射_C++_Boost Bind_Boost Function - Fatal编程技术网

C++ boost::绑定并插入boost::无序映射

C++ boost::绑定并插入boost::无序映射,c++,boost-bind,boost-function,C++,Boost Bind,Boost Function,我想使用boost::bind创建一个boost::function将一个新的键值对插入到boost::unorered\u映射中,但我得到的编译错误很少 typedef boost::unordered_map< std::string, std::string > dict_type; inline void insert( const std::string& key, const std::string& value ){ ty

我想使用
boost::bind
创建一个
boost::function
将一个新的键值对插入到
boost::unorered\u映射中
,但我得到的编译错误很少

typedef boost::unordered_map< 
        std::string, std::string > dict_type;


inline void insert( const std::string& key, const std::string& value ){

    typedef std::pair<dict_type::iterator, bool> out_type;

    dict_type::value_type to_insert(key,value);

    boost::function<void()> f = boost::bind<out_type>( 
        &dict_type::insert
        ,obj_
        ,boost::cref(to_insert)
    );
}
建议您有时可以通过将指向成员函数的指针强制转换为所需类型来解决重载函数的问题。使用一个临时变量来阻止它变得完全不可读,它看起来像:

typedef std::pair<typename dict_type::iterator, bool> out_type;

typename dict_type::value_type to_insert(key,value);

out_type (dict_type::*ins) (typename dict_type::value_type const&) const = &dict_type::insert;

boost::function<void()> f = boost::bind(
    ins
    ,obj_
    ,boost::cref(to_insert)
);
typedef std::配对类型;
typename dict_type::value_type to_insert(键,值);
out_type(dict_type::*ins)(typename dict_type::value_type const&)const=&dict_type::insert;
boost::函数f=boost::bind(
ins
,obj_
,boost::cref(to_insert)
);

问题在于
boost::unordered\u map
包含多个
insert
,因此
&dict\u type::insert
不明确。最简单的解决方案是定义一个函数来调用正确的重载:

void insert(dict_type & dict, dict_type::value_type const & value) {
    dict.insert(value);
}

boost::function<void()> f = boost::bind( 
    insert
    ,boost::ref(obj_)
    ,boost::cref(to_insert)
);
void insert(dict_type&dict,dict_type::value_type const&value){
插入指令(值);
}
boost::函数f=boost::bind(
插入
,boost::ref(对象)
,boost::cref(to_insert)
);
或者可以显式指定重载:

boost::function<void()> f = boost::bind( 
    static_cast<out_type (dict_type::*)(dict_type::value_type const &)>(&dict_type::insert)
    ,obj_
    ,boost::cref(to_insert)
);
boost::function f=boost::bind(
静态类型转换(&dict类型::插入)
,obj_
,boost::cref(to_insert)
);
在C++11中,可以避免lambda的问题:

std::function<void()> f = [&](){obj_.insert(to_insert);};
std::函数f=[&](){obj_u.insert(to_insert);};

请指定您的编译器版本好吗?一些不理解部分专门化的旧编译器不能接受指向函数成员的指针作为
boost::bind
的第一个参数。我说您的解决方案使用静态类型转换更快是错误的吗?通过使用static_cast,可在编译时解决歧义,因此即使编写的时间更长,也会使绑定调用更快,不是吗?理论上,
static_cast
会更快(这与我的示例相当),但一些编译器可能能够优化这个微不足道的函数,所以他们可能会得出相同的结果。哦..我找到了..应该是-,就像第二个论点一样。我读了你建议的页面,现在我更明白为什么bind不起作用了。谢谢。
std::function<void()> f = [&](){obj_.insert(to_insert);};