填充增强的的std ::地图::与升压::分配:: map_list_of功能(Populating

2019-09-30 08:10发布

我想创建一个键值的数据结构,将是应对字符串匹配的正则表达式来模式的事件非常有用。 所以我试图用一个Boost库去解决它:

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

//...

}

当我尝试编译它(不幸的是我不得不用很旧的软件 - GCC 4.4.7,提高1.41,因此C ++ 11的支持是非常有限的):

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

编译失败,此错误:

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

好像我的问题是接近这一个 ,但是,它并没有给我灵感。 这将是很好,如果有人可以告诉我的错误。

答案 @sehe已经提到的,这个代码是正确的,最多最新版本的gcc,但不能用gcc 4.4来处理。 不幸的是我。

Answer 1:

我会说,应该只是编译。

你可以尝试更直接的方式(不绑定),虽然,这可能是编译器(很多)更容易:

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

编辑由于没有工作,尽量使之更明确的还是:

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


文章来源: Populating the std::map of boost::function with boost::assign::map_list_of