Populating the std::map of boost::function with bo

2019-07-26 13:48发布

问题:

I would like to create a key-value data structure that would be useful for responding to the events of string matching to regex patterns. So I am trying to work it out with a Boost library:

#include <boost/regex.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/assign.hpp>
#include <map>

typedef std::map<boost::regex, boost::function<void(void)> > regex_callback;

void SuOpenedCallback()
{
}

void SuClosedCallback()
{
}


int main(int argc, char** argv)
{
    //Don't pay attention to these definitions... 
    boost::regex su_opened  ("^(\\w+)\\s(\\d+)\\s([\\d:]+)\\s(\\w+)\\s"
                            "su: pam_unix\\(su:session\\): session opened for user{1}\\s([\\w\\d]+)\\sby{1}\\s([\\w\\d]+)\\(uid=([\\d]+)\\)$"); 
    boost::regex su_closed  ("^(\\w+)\\s(\\d+)\\s([\\d:]+)\\s(\\w+)\\s"
                            "su: pam_unix\\(su:session\\): session closed for user{1}\\s([\\w\\d]+)$");

    //Compilation error:
    regex_callback resolver = boost::assign::map_list_of<boost::regex, boost::function<void(void)> >
        (su_opened, boost::bind(&SuOpenedCallback))
        (su_closed, boost::bind(&SuClosedCallback));

//...

}

When I try to compile it (unfortunately I am forced to use quite old software - gcc 4.4.7, boost 1.41, therefore C++11 support is very limited):

 g++ boost_regexp.cpp -o test -lboost_regex -std=c++0x

compilation fails with this error:

boost_regexp.cpp: In function ‘int main(int, char**)’: boost_regexp.cpp:58: error: ambiguous overload for ‘operator=’ in ‘resolver = boost::assign::map_list_of(const Key&, const T&) [with Key = boost::regex, T = boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>](((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&)((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>*)(& boost::bind [with R = void](SuOpenedCallback))))).boost::assign_detail::generic_list<T>::operator() [with U = boost::regex, U0 = boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>, T = std::pair<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>](((const boost::regex&)((const boost::regex*)(& su_closed))), ((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&)((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>*)(& boost::bind [with R = void](SuClosedCallback)))))’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:251: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:266: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:286: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::initializer_list<std::pair<const _Key, _Tp>) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] make: *** [all] Error 1

Seems like my problem is close to this one, however, it does not inspire me. It would be nice if someone could show me the mistake.

ANSWER As @sehe has mentioned, this code is correct for up-to-date versions of gcc but cannot be handled with gcc 4.4. Unfortunately for me.

回答1:

I'd say that should just compile.

You might try the more direct approach (without bind) though, which is probably (a lot) easier on the compiler:

regex_callback resolver = boost::assign::map_list_of<boost::regex, boost::function<void(void)> >
    (su_opened, &SuOpenedCallback)
    (su_closed, &SuClosedCallback);

EDIT Since that didn't work, try making it more explicit still:

typedef boost::function<void(void)> Func;

regex_callback resolver = boost::assign::map_list_of<boost::regex, Func >
    (su_opened, Func(boost::bind(&SuOpenedCallback)))
    (su_closed, Func(boost::bind(&SuClosedCallback)));

// or:
regex_callback resolver = boost::assign::map_list_of<boost::regex, Func >
    (su_opened, Func(&SuOpenedCallback))
    (su_closed, Func(&SuClosedCallback));