0

に新しいキーと値のペアを挿入するboost::bindために使用したいのですが、コンパイルエラーがほとんど発生しませんでした。boost::functionboost::unoredered_map

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)
    );
}

以下のエラーbindは、の適切なオーバーロードが見つからないようですunordered_map::insert。この場合、正確に正しいオーバーロードを指定しますが、今回は機能しません。あなたはそれが何であるか知っていますか?

 ../include2/llve_clorder_id.h:32: error: no matching function for call to 
'bind(<unresolved overloaded function type>,
 boost::unordered_map<std::basic_string<char, std::char_traits<char>, 
 std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
 std::allocator<char> >, boost::hash<std::basic_string<char, std::char_traits<char>,   
 std::allocator<char> > >, std::equal_to<std::basic_string<char, std::char_traits<char>, 
 std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, 
 std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
 std::char_traits<char>, std::allocator<char> > > > >&, const 
 boost::reference_wrapper<const std::pair<const std::basic_string<char, 
 std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
 std::char_traits<char>, std::allocator<char> > > >)'
4

2 に答える 2

1

http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#Troubleshootingは、pointer-to-member-functionを目的のタイプにキャストすることで、オーバーロードされた関数の問題を回避できる場合があることを示しています。 。一時変数を使用して完全に読み取れなくなるのを防ぐと、次のようになります。

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)
);
于 2012-03-08T18:31:21.977 に答える
1

問題は、boost::unordered_map複数が含まれているinsertため、&dict_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)
);

または、オーバーロードを明示的に指定することもできます。

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)
);

C ++ 11では、ラムダの問題を回避できます。

std::function<void()> f = [&](){obj_.insert(to_insert);};
于 2012-03-08T18:39:24.100 に答える